这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions persistence-modules/core-java-persistence-4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.baeldung.resusepreparedstatement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReusePreparedStatement {

private Connection connection = null;
private static final String SQL = "INSERT INTO CUSTOMER (id, first_name, last_name) VALUES(?,?,?)";

public void setupDatabaseAndConnect() throws SQLException {
connection = DriverManager.getConnection("jdbc:h2:mem:testDB", "dbUser", "dbPassword");
String createTable = "CREATE TABLE CUSTOMER (id INT, first_name TEXT, last_name TEXT)";
connection.createStatement()
.execute(createTable);
}

public void destroyDB() throws SQLException {
String destroy = "DROP table IF EXISTS CUSTOMER";
connection.prepareStatement(destroy)
.execute();
connection.close();
}

public void inefficientUsage() throws SQLException {
for (int i = 0; i < 10000; i++) {
PreparedStatement preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setInt(1, i);
preparedStatement.setString(2, "firstname" + i);
preparedStatement.setString(3, "secondname" + i);
preparedStatement.executeUpdate();
preparedStatement.close();
}
}

public void betterUsage() {
try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) {
for (int i = 0; i < 10000; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setString(2, "firstname" + i);
preparedStatement.setString(3, "secondname" + i);
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public void bestUsage() {
try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) {
connection.setAutoCommit(false);
for (int i = 0; i < 10000; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setString(2, "firstname" + i);
preparedStatement.setString(3, "secondname" + i);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
try {
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public int checkRowCount() {
try (PreparedStatement counter = connection.prepareStatement("SELECT COUNT(*) AS customers FROM CUSTOMER")) {
ResultSet resultSet = counter.executeQuery();
resultSet.next();
int count = resultSet.getInt("customers");
resultSet.close();
return count;
} catch (SQLException e) {
return -1;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.reusepreparedstatement;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.sql.SQLException;

import org.junit.jupiter.api.Test;

import com.baeldung.resusepreparedstatement.ReusePreparedStatement;

class ReusePreparedStatementUnitTest {

@Test
void whenCallingInefficientPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
ReusePreparedStatement service = new ReusePreparedStatement();
service.setupDatabaseAndConnect();
service.inefficientUsage();
int rowsCreated = service.checkRowCount();
service.destroyDB();
assertEquals(10000, rowsCreated);
}

@Test
void whenCallingBetterPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
ReusePreparedStatement service = new ReusePreparedStatement();
service.setupDatabaseAndConnect();
service.betterUsage();
int rowsCreated = service.checkRowCount();
service.destroyDB();
assertEquals(10000, rowsCreated);
}

@Test
void whenCallingBestPreparedStatementMethod_thenRowsAreCreatedAsExpected() throws SQLException {
ReusePreparedStatement service = new ReusePreparedStatement();
service.setupDatabaseAndConnect();
service.bestUsage();
int rowsCreated = service.checkRowCount();
service.destroyDB();
assertEquals(10000, rowsCreated);
}

}