Skip to content

Commit 4d80b0c

Browse files
Merge pull request #18412 from sam-gardner/BAEL-6879-reusing-preparedstatement-multiple-times
BAEL-6879 Reusing a PreparedStatement multiple times example code and tests
2 parents 7a526ad + 6e38d87 commit 4d80b0c

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

persistence-modules/core-java-persistence-4/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
<plugin>
6262
<groupId>org.apache.maven.plugins</groupId>
6363
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>17</source>
66+
<target>17</target>
67+
</configuration>
6468
</plugin>
6569
</plugins>
6670
</build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.reusepreparedstatement;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.sql.SQLException;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import com.baeldung.resusepreparedstatement.ReusePreparedStatement;
10+
11+
class ReusePreparedStatementUnitTest {
12+
13+
@Test
14+
void whenCallingInefficientPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
15+
ReusePreparedStatement service = new ReusePreparedStatement();
16+
service.setupDatabaseAndConnect();
17+
service.inefficientUsage();
18+
int rowsCreated = service.checkRowCount();
19+
service.destroyDB();
20+
assertEquals(10000, rowsCreated);
21+
}
22+
23+
@Test
24+
void whenCallingBetterPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
25+
ReusePreparedStatement service = new ReusePreparedStatement();
26+
service.setupDatabaseAndConnect();
27+
service.betterUsage();
28+
int rowsCreated = service.checkRowCount();
29+
service.destroyDB();
30+
assertEquals(10000, rowsCreated);
31+
}
32+
33+
@Test
34+
void whenCallingBestPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
35+
ReusePreparedStatement service = new ReusePreparedStatement();
36+
service.setupDatabaseAndConnect();
37+
service.bestUsage();
38+
int rowsCreated = service.checkRowCount();
39+
service.destroyDB();
40+
assertEquals(10000, rowsCreated);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)