About Statement object:
As mentioned earlier, the Statement object is used to bind the operations to be performed, and there are three execution methods on it: 用 to execute the executEcuteQuery () used to perform update operations () and use to perform dynamics Unknown operation of Execute ().
JDBC does not detect the SQL statement to be executed when compiling, just look at a string, only if the driver is executed when the SQL statement is not known or not.
A Statement object can only have a result set in the event. This is tolerance, that is, even if there is no CLOSE () method that calls the Resultset () method, simply open the second result set closes the last result set. So if you want to operate multiple result sets at the same time, create multiple statement objects. If you don't need to operate simultaneously, you can perform multiple result sets on a statement object.
Here I have to specify it, many people will nested inquiry with a statement, and then ask me to say why I can't loop? The truth has been clear. Let's analyze the nested query:
Connection conn = NULL;
Statement Stmt = NULL;
CONN = .......;
Stmt = conm.createstatement (xxxxxx);
ResultSet RS = Stmt.executeQuery (SQL1);
While (rs.next ()) {
Str = rs.getstring (xxxxx);
ResultSet RS1 = Stmt.executeQuery (/ "SELECT * FROM Table WHERE Field = STR /");
}
When Stmt.executeQuery (/ "SELECT * FROM table where field = str /"); when assigned to RS1, this hidden operation is that RS has been turned off, can you loop it? So if you want to operate multiple The result set must let it bind to different Statement objects. Good in a Connection object, you can create any multiple Statement objects without requiring you to reagent.
Options for obtaining and setting Statement: Just look at its getXxx method and setXX method, it will be understood as the basic knowledge, just mention the following:
SetQueryTimeout, set the timeout limit for SQL execution.
SetMaxRows, set the number of rows that result.
SetScapeProcessing, if the parameter is True, the driver is replaced before sending the SQL statement to the database, otherwise let the database handles yourself, of course, these default values can be queried by the GET method.
Statement's two subclasses:
PreparedStatement: For multiple executions of the same statement, STATEMENT sends the SQL statement to the database each time, this efficiency is significantly low, and if the database supports pre-compiled, preparedStatement first will send the statement to be executed once, Then each execution does not have to send the same statement, the efficiency is of course improved, of course, if the database does not support precompilation, PREPAREDStatement will work as Statement, just not high efficiency without the need for user-purpose intervention.
In addition, PreparedStatement also supports reception parameters. It can be implemented only after the transmission of different parameters after the precompile.
PreparedStateMent PS = Conn.PrepareStatement (/ "SELECT * FROM Table WHERE field =? /");
ps.setstring (1, parameters);
ResultSet RS = ps.executeQuery ();
CallableStatement: is a subclass of preparedStatement, which is just used to perform stored procedures.
CallableStatement sc = conn.preparecall (/ "{call query ()} /");
ResultSet RS = cs.executeQuery ();