SELECT query using executeUpdate() instead of executeQuery()
for Ex:
class sample {
public static void main(String a[]) {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:orcl", "", "");
String str = "Slect * from EMP";
Statement st = con.createStatement();
try {
st.exe开发者_如何学PythoncuteUpdate("select * from EMP"); //gives us Exception } catch(SQLException ex) { // I want actuval code here.......... //CODE here............
}//catch}//try}//main}//class
As others have pointed out: executeUpdate()
can not be used to run queries.
If you are looking for a way to execute statement without knowing what they do, you should have a look at the execute() method.
http://download.oracle.com/javase/6/docs/api/java/sql/Statement.html#execute%28java.lang.String%29
The returned boolean will tell you if it returned a result or just update counts. You can then use getResultSet() to obtain the result or getUpdateCount() to get number of affected rows.
Note that a statement is allowed to return more than one result and/or udpate count. See the example in getMoreResults().
the below code exlpains that the execute update statement gives exception in the case of
JdbcOdbcDriver
but not in case of OracleDriver
so it is not always necesarry that select statement will give exception in executeUpdate("Select * ...");
but it depends on the Drive we register in DriverManager.registerDriver(Driver ob);
some Driver May give Exception while some will not
but the answer to your question is you should not use executeUpdate("Sel..") for select Statement even if doesn't give Exception read the below code and and comment you will understand better
import java.sql.*;
import sun.jdbc.odbc.*;
import oracle.jdbc.driver.*;
public class MyDb {
public static void main(String args[]) throws Exception {
//drive is oracle.jdbc.driver.OracleDriver;
OracleDriver od = new OracleDriver();
DriverManager.registerDriver(od);
Connection conn;
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
Statement stm = conn.createStatement();
int val = stm.executeUpdate("select * from mylog");
System.out.println(val);
//output for above code is 10 actually the table had 15 rows
//but when table had 7 rows output was 7 ,when number of rows where 9 output was 9
//but when the number of row in table were more than or equal to 10 the out put was 10
//so actually it is no meaning to use select statement within executeQuery
//even if it doesn't give exception
//driver is sun.jdbc.odbc.JdbcOdbcDriver;
JdbcOdbcDriver od2 = new JdbcOdbcDriver();
DriverManager.registerDriver(od2);
Connection conn2 = DriverManager.getConnection("jdbc:odbc:swap", "system", "system");
Statement stm2 = conn2.createStatement();
int val2 = stm2.executeUpdate("select * from mylog");
//while this code gives exception
//Exception in thread "main" java.sql.SQLException: No row count was produced
// at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
// at MyDb.main(MyDb.java:19)
System.out.println(val2);
}
}
executeUpdate
is intended for statements that modifies data (update, insert). This is why you get an exception /by the way why do you want to use executeUpdate
here?)
String str = "Slect * from EMP";
Should be
String str = "Select * from EMP";
as well.
精彩评论