1: differences during creation:
Statement STM = con. createstatement ();
Preparedstatement PSTM = con. preparestatement (SQL );
During execution:
Cmd.exe cute (SQL );
Pcmd.exe cute ();
2: Once PSTM is bound to an SQL statement, PSTM cannot execute other SQL statements, that is, only one SQL command can be executed.
STM can execute multiple SQL commands.
3: for SQL statements with homogeneous execution (only with different values and the same structure), the execution efficiency of PSTM is relatively high. For heterogeneous SQL statements, the execution efficiency of statement is high.
4: when external variables are required, PSTM is executed more efficiently.
The following is a statement column:
Java code
Package com. JDBC. Proc;
Import java. SQL .*;
Public class statementtest {
Public static void main (string ARGs []) {
Connection conn = NULL;
Statement STM = NULL;
Resultset rs = NULL;
Try {
Conn = dbtool. getconnection ();
String SQL = "select empno, ename from EMP" +
"Where e-mapreduce = 7499 ";
STM = conn. createstatement ();
Rs1_cmd.exe cutequery (SQL );
While (Rs. Next ()){
System. Out. println (Rs. getint (1) + "---" + Rs. getstring (2 ));
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Dbtool. Release (RS, STM, Conn );
}
}
}
His main role is to explain the usage of statement.
The following is a column about preparestatement:
Java code
Package com. JDBC. Proc;
Import java. SQL .*;
Public class preparestatement {
Public static void main (string [] ARGs ){
Connection conn = NULL;
Preparedstatement psmt = NULL;
Resultset rs = NULL;
Try {
Conn = dbtool. getconnection ();
String SQL = "select empno, ename" +
"From EMP" +
"Where empno =? ";
Psmt = conn. preparestatement (SQL );
Psmt. setint (1, 7499 );
Rsw.psmt.exe cutequery ();
While (Rs. Next ()){
System. Out. println (Rs. getint (1) + "---" + Rs. getstring (2 ));
}
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Dbtool. Release (RS, psmt, Conn );
}
}
}
Differences between preparestatement and statement