Turn: http://blog.csdn.net/colin_fantasy/article/details/3898070
Differences between execute, executequery, and executeupdate
In JDBC, the statement interface provides three methods for executing SQL statements: executequery, executeupdate, and execute. The method used is determined by the content generated by the SQL statement.
1> method executequery
A statement used to generate a single result set, such as a SELECT statement. The most commonly used method for executing SQL statements. This method is used to execute select statements, which are almost the most commonly used SQL statements. However, only query statements can be executed. After execution, the resultset object representing the query result is returned.
For example:
// Load the database Driver Class. forname ("com. MySQL. JDBC. Driver ");
// Use drivermanager to obtain the database connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/test", "root", "1234"); // use connection to create a statment object statement stmt = Conn. createstatement (); // execute the query statement resultset Rs into stmt.exe cutequery ("select * from Teacher"); // output the query result to while (RS. next () {system. out. println (RS. getint (1) + "/t" + Rs. getstring (2 ));}
2> method executeupdate
It is used to execute insert, update, or delete statements and SQL DDL (Data Definition Language) statements, such as CREATE TABLE and drop table. The insert, update, or delete statement modifies one or more columns in zero or multiple rows of the table. The returned value of executeupdate is an integer (INT) that indicates the number of affected rows (that is, the update count ). For statements that do not operate on rows such as create table or drop table, the return value of executeupdate is always zero.
For example:
// Load the database Driver Class. forname ("com. mySQL. JDBC. driver "); // use drivermanager to obtain the database connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/test", "root", "1234"); // use connection to create a statment object statement stmt = Conn. createstatement ();
// Execute the DML statement and return the number of affected records. Return stmt.exe cuteupdate (SQL );
3> method execute:
It can be used to execute any SQL statement and return a Boolean value, indicating whether the resultset is returned when the SQL statement is executed. If the first result after execution is resultset, true is returned; otherwise, false is returned. However, it is troublesome to execute SQL statements. Generally, we do not need to use the execute method to execute SQL statements, but it is more suitable to use executequery or executeupdate, however, if you are not clear about the SQL statement type, you can only use the execute method to execute the SQL statement.
For example:
// Load the driver class. forname (driver); // obtain the database connection conn = drivermanager. getconnection (URL, user, pass); // use connection to create a statment object stmt = Conn. createstatement (); // run the SQL statement. A boolean value is returned to indicate whether the resultset Boolean hasresultset = stmt.exe cute (SQL) is included. // if the execution result is followed by the resultset result set if (hasresultset) {// obtain the result set rs = stmt. getresultset (); // resultsetmetadata is the metadata interface used to analyze the result set. resultsetmetadata rsmd = Rs. getmetadata (); int columncount = rsmd. getcolumncount (); // iteratively outputs the resultset object while (RS. next () {// output the values of each column in sequence for (INT I = 0; I <columncount; I ++) {system. out. print (RS. getstring (I + 1) + "/t");} system. out. print ("/N") ;}} else {system. out. println ("records affected by the SQL statement" + stmt. getupdatecount () + "entry ");}
Difference between execute, executequery, and executeupdate