Packagecn. pdispose; importjava. SQL. connection; importjava. SQL. preparedStatement; importjava. SQL. SQLException; importjava. SQL. statement; importcn. paging. jdbcUtil; batch processing publicclassDispose {privatestaticConnectionconnnull; p
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; p
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 ();
}
}