To improve performance when saving large volumes of data to a database, data is usually submitted in batch insert mode, instead of executing an insert operation for each piece of data. Below is an example of batch data insertion into an Oracle database using JDBC:
Example
| The Code is as follows: |
Copy code |
String SQL = "insert into record (id, record, insetDate) values (sequence_record.nextval ,?, Sysdate )";
Connection connection = DBConnection. getConnection ();
PreparedStatement pstmt = connection. prepareStatement (SQL );
// Maximum number of entries submitted each time
Final int batchSize = 5000;
Int count = 0;
For (Record record: recordList ){
Pstmt. setString (1, record. getRecord ());
Pstmt. addBatch ();
If (++ count % batchSize = 0 ){
Pstmt.exe cuteBatch ();
}
}
// Submit the remaining data
Pstmt.exe cuteBatch ();
Pstmt. close ();
Connection. close (); |