Batch Processing and transaction processing, batch processing

Source: Internet
Author: User

Batch Processing and transaction processing, batch processing

Batch Processing is to issue multiple query commands to the database at a time and execute them together.
Method defined by the Statement interface:
|-Added the batch processing statement:

 public void addBatch(String sql)

 


|-Execute batch processing:

public int [] executeBatch()throws SQLException


Methods defined by the PreparedStatement interface:
Add batch processing: public void addBatche () throws SQLexceeption


Example:

Public class TestConn {
Static final String DBDRIVER = "oracle. jdbc. driver. OracleDriver ";
Static final String url = "jdbc: oracle: thin :@ localhost: 1521: ORCL ";
Private static final String USER = "***";
Private static final String PASSWORD = "***";
Public static void main (String [] args) throws Exception {
Class. forName (DBDRIVER );
Connection conn = DriverManager. getConnection (url, USER, PASSWORD );
Statement stmt = conn. createStatement ();
Conn. setAutoCommit (false );
Try {// Add data
Stmt. addBatch ("Insert into myemp (ename, job) values ('sdvb1 ', 'test ad ')");
Stmt. addBatch ("Insert into myemp (ename, job) values ('sdvb2', 'test ad ')");
Stmt. addBatch ("Insert into myemp (ename, job) values ('sdvb3', 'test ad ')");
Stmt. addBatch ("Insert into myemp (ename, job) values ('sdvb4', 'test ad ')");
Int resultbatch ();
System. out. println (Arrays. toString (result ));
Conn. commit (); // No error, submit data
} Catch (Exception e ){
E. printStackTrace ();
Conn. rollback (); // if an error occurs, roll back the data.
}
Conn. close ();
}
}

 

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.