The main methods used are:
Preparedstatement.executebatch ();//Accumulated Data execution
Preparedstatement.clearbatch ();//The accumulation of the clearing off
Preparedstatement.addbatch ();//This is not done immediately, after accumulating to a certain number, refresh the execution
-----------------------------------------------------------------------------------------------
Test12 t=new Test12 ();
/*
* Batch processing data JDBC statement, improve processing speed
* */
Inserting data
@Test
public void Testbase () throws exception{
Connection Connection=null;
PreparedStatement Preparedstatement=null;
String Sql=null;
try {
Connection=t.getconnection ();
Start thing cancel Default Commit
Setautocommit (connection);
Sql= "INSERT into customer where values (?,?,?,?)";
Preparedstatement=connection.preparestatement (SQL);
Date Date=new Date (new Java.util.Date (). GetTime ());
Long Began=system.currenttimemillis ();
for (int i=0;i<100000;i++) {
Preparedstatement.setint (1, i+1);
Preparedstatement.setstring (2, "name" +i);
Preparedstatement.setstring (3, "email" + 1);
Preparedstatement.setdate (4, date);
Preparedstatement.executequery ();
There is no immediate execution, and after a certain amount has been accumulated, the execution is refreshed
Preparedstatement.addbatch ();
if ((i+1)%300==0) {
Preparedstatement.executebatch ();//Accumulated Data execution
Preparedstatement.clearbatch ();//Accumulate the clear
}
}
The last is not an integer of 300, and then it executes once
if (1000000%300!=0) {
Preparedstatement.executebatch ();
Preparedstatement.clearbatch ();
}
Long End=system.currenttimemillis ();
System.out.println (End-began);
to commit things.
commit (connection);
} catch (Exception e) {
}finally {//rollback things
Rollbank (connection);
T.close (connection, preparedstatement, null);
}
}
Start things: Cancel default commits
public void Setautocommit (Connection Connection) {
if (connection!=null) {
try {
Connection.setautocommit (FALSE);
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
have successfully submitted things
public void commit (Connection Connection) {
if (connection!=null) {
try {
Connection.commit ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
Rolling back things
public void Rollbank (Connection Connection) {
if (connection!=null) {
try {
Connection.rollback ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
public void Testsettransactiontsolation () {
Connection Connection=null;
PreparedStatement Preparedstatement=null;
ResultSet Resultset=null;
try {
Connection=t.getconnection ();
Settings are not auto-committed
Connection.setautocommit (FALSE);
String sql1= "Update test set grade= grade+100 where flow_id=3";
T1.update (connection, SQL1);
have successfully submitted things
Connection.commit ();
} catch (Exception e) {
E.printstacktrace ();
}finally {
}
}
Batch processing data for JDBC