Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void addToBatch(String sql) throws SQLException{
- sttmnt.addBatch(sql);
- batchSize++;
- if (batchSize == elementsPerExecute){
- executeBatches();
- }
- }
- try {
- connection con.setAutoCommit(false);
- PreparedStatement prepStmt = con.prepareStatement(
- "UPDATE DEPT SET MGRNO=? WHERE DEPTNO=?");
- prepStmt.setString(1,mgrnum1);
- prepStmt.setString(2,deptnum1);
- prepStmt.addBatch();
- prepStmt.setString(1,mgrnum2);
- prepStmt.setString(2,deptnum2);
- prepStmt.addBatch();
- int [] numUpdates=prepStmt.executeBatch();
- for (int i=0; i < numUpdates.length; i++) {
- if (numUpdates[i] == -2)
- System.out.println("Execution " + i +
- ": unknown number of rows updated");
- else
- System.out.println("Execution " + i +
- "successful: " numUpdates[i] + " rows updated");
- }
- con.commit();
- } catch(BatchUpdateException b) {
- // process BatchUpdateException
- }
- Sring query = "INSERT INTO users (id, user_name, password) VALUES(?,?,?)";
- PreparedStatement statement = connection.preparedStatement(query);
- for(User user: userList){
- statement.setString(1, user.getId()); //1 is the first ? (1 based counting)
- statement.setString(2, user.getUserName());
- statement.setString(3, user.getPassword());
- statement.addBatch();
- }
- statement.executeBatch();
- statement.clearBatch(); //If you want to add more,
- //(so you don't do the same thing twice)
Add Comment
Please, Sign In to add comment