Differences between preparestatement and statement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.