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 ();
}
}