Skip to content

Commit 7cd54ff

Browse files
committed
https://jira.baeldung.com/browse/BAEL-8292
1 parent c21bb1d commit 7cd54ff

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

persistence-modules/my-sql/src/main/java/com/baeldung/connectingtoremotemysqlssh/RemoteMysqlConnection.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import com.jcraft.jsch.JSch;
44
import com.jcraft.jsch.JSchException;
55
import com.jcraft.jsch.Session;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
6+
87
import java.sql.Connection;
98
import java.sql.DriverManager;
109
import java.sql.SQLException;
@@ -49,20 +48,23 @@ public static Connection databaseConnection(int port) throws SQLException {
4948

5049
public static void createTable(Connection connection, String tableName) throws SQLException {
5150
String createTableSQL = "CREATE TABLE " + tableName + " (id INT, data VARCHAR(255))";
52-
Statement statement = connection.createStatement();
53-
statement.execute(createTableSQL);
51+
try (Statement statement = connection.createStatement()) {
52+
statement.execute(createTableSQL);
53+
}
5454
}
5555

5656
public static void insertData(Connection connection, String tableName) throws SQLException {
5757
String insertDataSQL = "INSERT INTO " + tableName + " (id, data) VALUES (1, 'test data')";
58-
Statement statement = connection.createStatement();
59-
statement.execute(insertDataSQL);
58+
try (Statement statement = connection.createStatement()) {
59+
statement.execute(insertDataSQL);
60+
}
6061
}
6162

6263
public static boolean isTableExists(Connection connection, String tableName) throws SQLException {
63-
Statement statement = connection.createStatement();
64-
ResultSet resultSet = statement.executeQuery("SHOW TABLES LIKE '" + tableName + "'");
65-
return resultSet.next();
64+
try (Statement statement = connection.createStatement()) {
65+
ResultSet resultSet = statement.executeQuery("SHOW TABLES LIKE '" + tableName + "'");
66+
return resultSet.next();
67+
}
6668
}
6769

6870
public static void disconnect(Session session, Connection connection) throws SQLException {

0 commit comments

Comments
 (0)