Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void executeSQL(List<String> data) {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
- e1.printStackTrace();
- }
- String connectionUrl = "jdbc:mysql://localhost:6000/user_data";
- String connectionUser = "test";
- String connectionPassword = "testtest123";
- String query = "INSERT INTO user_promocode(user_id, promocode) VALUES(?,?)";
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
- conn.setAutoCommit(false);
- try (PreparedStatement stmt = conn.prepareStatement(query)) {
- for(String userId : data) {
- stmt.setString(1, userId);
- stmt.setString(2, "PRE10");
- stmt.addBatch();
- stmt.setString(1, userId);
- stmt.setString(2, "POST10");
- stmt.addBatch();
- }
- int[] updateCounts = stmt.executeBatch();
- System.out.println("update counts: " + updateCounts);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(conn != null) {
- try {
- conn.commit();
- conn.setAutoCommit(true);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment