|
3 | 3 | import com.jcraft.jsch.JSch;
|
4 | 4 | import com.jcraft.jsch.JSchException;
|
5 | 5 | import com.jcraft.jsch.Session;
|
6 |
| -import org.slf4j.Logger; |
7 |
| -import org.slf4j.LoggerFactory; |
| 6 | + |
8 | 7 | import java.sql.Connection;
|
9 | 8 | import java.sql.DriverManager;
|
10 | 9 | import java.sql.SQLException;
|
@@ -49,20 +48,23 @@ public static Connection databaseConnection(int port) throws SQLException {
|
49 | 48 |
|
50 | 49 | public static void createTable(Connection connection, String tableName) throws SQLException {
|
51 | 50 | 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 | + } |
54 | 54 | }
|
55 | 55 |
|
56 | 56 | public static void insertData(Connection connection, String tableName) throws SQLException {
|
57 | 57 | 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 | + } |
60 | 61 | }
|
61 | 62 |
|
62 | 63 | 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 | + } |
66 | 68 | }
|
67 | 69 |
|
68 | 70 | public static void disconnect(Session session, Connection connection) throws SQLException {
|
|
0 commit comments