Statement and PreparedStatement execute multiple SQL

Source: Internet
Author: User

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

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.