Database Batch Processing

Source: Internet
Author: User

Package cn. Pdispose;


Import java. SQL. Connection;
Import java. SQL. PreparedStatement;
Import java. SQL. SQLException;
Import java. SQL. Statement;


Import cn. paging. JdbcUtil;


// Batch Processing
Public class Dispose {
Private static Connection conn = null;
Private static Statement st = null;
Private static PreparedStatement ps = null;
Public static void testInsert (){
Try {
Conn = JdbcUtil. getConnection ();
St = conn. createStatement ();
String sql1 = "insert into t_name (id, name) values (1, 'jing ')";
String sql2 = "insert into t_name (id, name) values (2, 'zhang ')";
String sql3 = "delete from t_name where id = 1 ";
// AddBatch () is a list added to the list.
St. addBatch (sql1 );
St. addBatch (sql2 );
St. addBatch (sql3 );
// Number of lines affected by each statement of the element division
Int [] I = st.exe cuteBatch ();
For (int num: I ){
System. out. println (num );
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
JdbcUtil. closeJdbc (conn, null, ps );
}
}

// Insert 100 data records into a table at the same time
// Because the statements are identical, but the parameters are different, PreparedStatement is used.
Public static void testInsert_1 (){
Long date = System. currentTimeMillis ();
Try {
Conn = JdbcUtil. getConnection ();
String SQL = "insert into t_name values (?,?) ";
Ps = conn. prepareStatement (SQL );
For (int I = 1; I <= 100; I ++ ){
Ps. setInt (1, I );
Ps. setString (2, "date ---" + I );
// Add a set of parameters to the batch processing command of this PreparedStatement object.
Ps. addBatch ();
}
// Submit a batch of commands to the database for execution. If all commands are successfully executed, an array consisting of update counts is returned.
Ps.exe cuteBatch ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
JdbcUtil. closeJdbc (conn, null, ps );
}
System. out. println ("time used:" + (System. currentTimeMillis ()-date)/1000) + "second ");
}

// Insert 10000 data entries into the database and set the cache Zone
Public static void testInsert_2 (){
// Initialize a time
Long time = System. currentTimeMillis ();
Try {
Conn = JdbcUtil. getConnection ();
String SQL = "insert into t_name values (?,?) ";
Ps = conn. prepareStatement (SQL );
For (int I = 1; I <1000001; I ++ ){
Ps. setInt (1, I );
Ps. setString (2, "date" + I );
Ps. addBatch ();
If (I % 1000 = 0 ){
Ps.exe cuteBatch ();
Ps. clearBatch (); // clear data
}
}
Ps.exe cuteBatch ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
JdbcUtil. closeJdbc (conn, null, ps );
}

System. out. println ("time used:" + (System. currentTimeMillis ()-time)/1000) + "second ");
}


Public static void main (String [] args ){
TestInsert_2 ();
}
}













Related Article

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.