Guest User

Untitled

a guest
Nov 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. public void executeSQL(List<String> data) {
  2. try {
  3. Class.forName("com.mysql.jdbc.Driver").newInstance();
  4. } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
  5. e1.printStackTrace();
  6. }
  7. String connectionUrl = "jdbc:mysql://localhost:6000/user_data";
  8. String connectionUser = "test";
  9. String connectionPassword = "testtest123";
  10.  
  11. String query = "INSERT INTO user_promocode(user_id, promocode) VALUES(?,?)";
  12. Connection conn = null;
  13. try {
  14. conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
  15. conn.setAutoCommit(false);
  16. try (PreparedStatement stmt = conn.prepareStatement(query)) {
  17. for(String userId : data) {
  18. stmt.setString(1, userId);
  19. stmt.setString(2, "PRE10");
  20. stmt.addBatch();
  21.  
  22. stmt.setString(1, userId);
  23. stmt.setString(2, "POST10");
  24. stmt.addBatch();
  25. }
  26. int[] updateCounts = stmt.executeBatch();
  27. System.out.println("update counts: " + updateCounts);
  28. }
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. } finally {
  32. if(conn != null) {
  33. try {
  34. conn.commit();
  35. conn.setAutoCommit(true);
  36. } catch (SQLException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment