|
| 1 | +package com.baeldung.resusepreparedstatement; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | + |
| 9 | +public class ReusePreparedStatement { |
| 10 | + |
| 11 | + private Connection connection = null; |
| 12 | + private static final String SQL = "INSERT INTO CUSTOMER (id, first_name, last_name) VALUES(?,?,?)"; |
| 13 | + |
| 14 | + public void setupDatabaseAndConnect() throws SQLException { |
| 15 | + connection = DriverManager.getConnection("jdbc:h2:mem:testDB", "dbUser", "dbPassword"); |
| 16 | + String createTable = "CREATE TABLE CUSTOMER (id INT, first_name TEXT, last_name TEXT)"; |
| 17 | + connection.createStatement() |
| 18 | + .execute(createTable); |
| 19 | + } |
| 20 | + |
| 21 | + public void destroyDB() throws SQLException { |
| 22 | + String destroy = "DROP table IF EXISTS CUSTOMER"; |
| 23 | + connection.prepareStatement(destroy) |
| 24 | + .execute(); |
| 25 | + connection.close(); |
| 26 | + } |
| 27 | + |
| 28 | + public void inefficientUsage() throws SQLException { |
| 29 | + for (int i = 0; i < 10000; i++) { |
| 30 | + PreparedStatement preparedStatement = connection.prepareStatement(SQL); |
| 31 | + preparedStatement.setInt(1, i); |
| 32 | + preparedStatement.setString(2, "firstname" + i); |
| 33 | + preparedStatement.setString(3, "secondname" + i); |
| 34 | + preparedStatement.executeUpdate(); |
| 35 | + preparedStatement.close(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void betterUsage() { |
| 40 | + try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) { |
| 41 | + for (int i = 0; i < 10000; i++) { |
| 42 | + preparedStatement.setInt(1, i); |
| 43 | + preparedStatement.setString(2, "firstname" + i); |
| 44 | + preparedStatement.setString(3, "secondname" + i); |
| 45 | + preparedStatement.executeUpdate(); |
| 46 | + } |
| 47 | + } catch (SQLException e) { |
| 48 | + throw new RuntimeException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public void bestUsage() { |
| 53 | + try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) { |
| 54 | + connection.setAutoCommit(false); |
| 55 | + for (int i = 0; i < 10000; i++) { |
| 56 | + preparedStatement.setInt(1, i); |
| 57 | + preparedStatement.setString(2, "firstname" + i); |
| 58 | + preparedStatement.setString(3, "secondname" + i); |
| 59 | + preparedStatement.addBatch(); |
| 60 | + } |
| 61 | + preparedStatement.executeBatch(); |
| 62 | + try { |
| 63 | + connection.commit(); |
| 64 | + } catch (SQLException e) { |
| 65 | + connection.rollback(); |
| 66 | + throw e; |
| 67 | + } |
| 68 | + } catch (SQLException e) { |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public int checkRowCount() { |
| 74 | + try (PreparedStatement counter = connection.prepareStatement("SELECT COUNT(*) AS customers FROM CUSTOMER")) { |
| 75 | + ResultSet resultSet = counter.executeQuery(); |
| 76 | + resultSet.next(); |
| 77 | + int count = resultSet.getInt("customers"); |
| 78 | + resultSet.close(); |
| 79 | + return count; |
| 80 | + } catch (SQLException e) { |
| 81 | + return -1; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments