Javaweb Learning Summary (36)--Batching with JDBC
In the actual project development, sometimes need to send a batch of SQL statement execution to the database, should avoid to send execution to the database, and should adopt the batch processing mechanism of JDBC, in order to improve the execution efficiency.
There are two ways in which JDBC implements batching: statement and PreparedStatement
I. Using statement to complete batch processing
1. Use the Statement object to add the SQL statement to execute in bulk, as follows:
1 Statement.addbatch (SQL1), 2 Statement.addbatch (SQL2), 3 Statement.addbatch (SQL3);
2. Execute batch SQL statement: Statement.executebatch ();
3, Clear Batch processing command: Statement.clearbatch ();
1.1.
using statement to complete the batch process example
1. Write the test SQL script to create the table
1 CREATE TABLE TESTBATCH2 (3 ID int primary key,4 name varchar (20) 5);
2. Write the test code as follows:
1 package Me.gacl.demo; 2 3 Import java.sql.Connection; 4 Import Java.sql.ResultSet; 5 Import java.sql.Statement; 6 Import Me.gacl.utils.JdbcUtils; 7 Import Org.junit.Test; 8 9/**10 * @ClassName: JDBCBATCHHANDLEBYSTATEMENT11 * @Description: Implementing JDBC Batch operations with statement * @author: Aloof Wolf * @date: 2014-9-20 pm 10:05:4514 *15 * * * public class Jdbcbatchhandlebystatement {@Test19 public void Testjdbcbatchh Andlebystatement () {Connection conn = null;21 Statement st = null;22 ResultSet rs = null;23 Try{24 conn = Jdbcutils.getconnection (); String sql1 = "INSERT into Testbatch (id,name) value S (1, ' AAA ') ";" String sql2 = "INSERT into Testbatch (Id,name) VALUES (2, ' BBB ')"; string sql3 = "in Sert into Testbatch (id,name) VALUES (3, ' CCC '); String sql4 = "INSERT into Testbatch (id,name) VALUES (4, ' DDD ') "; String sql5 =" Update testbatch set name= ' GaCl ' where id=1 ";Tring sql6 = "INSERT into Testbatch (id,name) VALUES (5, ' FFF ')"; String sql7 = "Delete from Testbatch where ID =2 "; + st = Conn.createstatement (); 33//Add SQL34 St.addbatch (SQL1) to be executed in bulk; 35 St.addbatch (SQL2), St.addbatch (SQL3), Notoginseng St.addbatch (SQL4); St.addbatch (SQL5); 39 St.addbatch (SQL6); St.addbatch (SQL7); 41//Execute batch SQL statement st.executebatch (); 43//Clear Batch command St.clearbatch ();}catch (Exception e) {e.printstacktrace () ;}finally{48 Jdbcutils.release (Conn, St, RS); 49}50}51}
1.2, using Statement.addbatch (SQL) method to achieve the pros and cons of batch processing
Batch processing using Statement.addbatch (SQL):
Pros: You can send multiple different SQL statements to the database.
Disadvantage: SQL statements are not precompiled.
When you send more than one statement to a database, but only a different SQL statement, you need to write a number of SQL statements repeatedly. For example:
1 insert INTO user (' AA ', ' 111 ') and 2 insert into user (Name,password) VALUES (' BB ', ' 222 ') and 3 insert INTO U Ser (Name,password) VALUES (' cc ', ' 333 '); 4 Insert into User (' Name,password ') VALUES (' dd ', ' 444 ');
Second, the use of PreparedStatement complete batch processing 2.1,
using PreparedStatement to complete the batch process example
The test code is as follows:
1 package Me.gacl.demo; 2 3 Import java.sql.Connection; 4 Import java.sql.PreparedStatement; 5 Import Java.sql.ResultSet; 6 Import Me.gacl.utils.JdbcUtils; 7 Import Org.junit.Test; 8 9/**10 * @ClassName: JDBCBATCHHANDLEBYSTATEMENT11 * @Description: Implementing JDBC Batch operations with Preparestatement * @author: Aloof Wolf 13 * @date: 2014-9-20 PM 10:05:4514 *15 * * * public class Jdbcbatchhandlebypreparestatement {@Test19 public void Testjdbcbatchhandlebypreparestatement () {Long starttime = System.currenttimemillis (); Connection Conn = null;22 PreparedStatement st = null;23 ResultSet rs = null;24 try{26 conn = Jdbcutils.getconnection (); String sql = "INSERT into Testbatch (id,name) VALUES (?,?)"; --st = conn.preparestatement (SQL), i=1;i<1000008;i++ for (int) {//i=1000 200030 St.setint (1, i); St.setstring (2, "AA" + i); St.addbatch (); 33 if (i%1000==0) {st.executebatch (); St.clearbatch (); 36 }37}38 St.executebatch ();}catch (Exception e) {e.printstacktrace (); }FINALLY{42 Jdbcutils.release (Conn, St, RS);}44 long endtime = System.currenttim Emillis (); System.out.println ("program takes Time:" + (Endtime-starttime)/1000 + "seconds!! "); 46}47}
2.2, using Preparedstatement.addbatch () method to achieve the advantages and disadvantages of batch processing
Using Preparedstatement.addbatch () to implement batch processing
Advantage: The post-compilation SQL statement is sent with high execution efficiency.
Disadvantage: You can only apply in batches that have the same SQL statement but different parameters. So this form of batching is often used to bulk insert data in the same table, or to bulk update the data for a table.
The content on the JDBC batch is summed up so much.
Javaweb Learning Summary (36)--Batching with JDBC