The difference between the two objects:
1.Statement It is more suitable for batch processing of different SQL, it does not provide preprocessing capabilities, performance is relatively low.
2.PreparedStatement It is suitable for performing the same batch processing, it provides preprocessing functionality, and the properties are relatively high. /** *
@param args *
@throws SQLException *
@throws classnotfoundexception */
Public
Static
void Main (string[] args)
throws ClassNotFoundException,SQLException { //define SQL statementsString SQL1 = "CREATE TABLE person (ID int,name varchar)";String sql2 = INSERT into person values (1, ' Tom ');String sql3 = INSERT into person values (2, ' Fox ');String sql4 = INSERT into person values (3, ' Tony ');String sql5 = "Update person set name= ' Zhang San ' where id=1";String sql6 = "Delete from person where id=3"; Connection conn = jdbcutils.
getconnection ();Statement st = Conn.createstatement (); //Add batch SQL St.addbatch (SQL1);St.addbatch (SQL2);St.addbatch (SQL3);St.addbatch (SQL4);St.addbatch (SQL5);St.addbatch (sql6); //Execute batch SQL St.executebatch ();St.clearbatch ();st.close ();conn.close (); }When using a JDBC jar package with a higher version, the add parameter opens the cache to add parameters to the URL:? useserverprepstmts=true&cacheprepstmts=true&rewritebatchedstatements=true /** *
@param args *
@throws SQLException *
@throws classnotfoundexception */
Public
Static
void Main (string[] args)
throws ClassNotFoundException,SQLException {String sqlString = INSERT into person values (?,?);Connection conn = jdbcutils.
getconnection ();PreparedStatement PST = conn.preparestatement (sqlString);
Long L = System.
Currenttimemillis ();
for (
int i = 0; i < 10000; i++) {Pst.setint (1, i);pst.setstring (2, "Name" + i);Pst.addbatch ();
if (i% = = 0) {Pst.executebatch ();Pst.clearbatch (); //Empty cache } }Pst.executebatch ();pst.close ();conn.close ();System.
out. println (System.
Currenttimemillis ()-L); }
Statement and PreparedStatement execute multiple SQL