Batch processing data for JDBC

Source: Internet
Author: User

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

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.