Guest User

Untitled

a guest
Jul 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. private void addToBatch(String sql) throws SQLException{
  2. sttmnt.addBatch(sql);
  3. batchSize++;
  4. if (batchSize == elementsPerExecute){
  5. executeBatches();
  6. }
  7. }
  8.  
  9. try {
  10. connection con.setAutoCommit(false);
  11. PreparedStatement prepStmt = con.prepareStatement(
  12. "UPDATE DEPT SET MGRNO=? WHERE DEPTNO=?");
  13. prepStmt.setString(1,mgrnum1);
  14. prepStmt.setString(2,deptnum1);
  15. prepStmt.addBatch();
  16.  
  17. prepStmt.setString(1,mgrnum2);
  18. prepStmt.setString(2,deptnum2);
  19. prepStmt.addBatch();
  20. int [] numUpdates=prepStmt.executeBatch();
  21. for (int i=0; i < numUpdates.length; i++) {
  22. if (numUpdates[i] == -2)
  23. System.out.println("Execution " + i +
  24. ": unknown number of rows updated");
  25. else
  26. System.out.println("Execution " + i +
  27. "successful: " numUpdates[i] + " rows updated");
  28. }
  29. con.commit();
  30. } catch(BatchUpdateException b) {
  31. // process BatchUpdateException
  32. }
  33.  
  34. Sring query = "INSERT INTO users (id, user_name, password) VALUES(?,?,?)";
  35. PreparedStatement statement = connection.preparedStatement(query);
  36. for(User user: userList){
  37. statement.setString(1, user.getId()); //1 is the first ? (1 based counting)
  38. statement.setString(2, user.getUserName());
  39. statement.setString(3, user.getPassword());
  40. statement.addBatch();
  41. }
  42.  
  43. statement.executeBatch();
  44. statement.clearBatch(); //If you want to add more,
  45. //(so you don't do the same thing twice)
Add Comment
Please, Sign In to add comment