refactor: use try-with-resources for cash id db operations
This commit is contained in:
@@ -19,16 +19,16 @@
|
|||||||
*/
|
*/
|
||||||
package client.inventory.manipulator;
|
package client.inventory.manipulator;
|
||||||
|
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import tools.DatabaseConnection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author RonanLana
|
* @author RonanLana
|
||||||
*/
|
*/
|
||||||
public class MapleCashidGenerator {
|
public class MapleCashidGenerator {
|
||||||
@@ -37,8 +37,8 @@ public class MapleCashidGenerator {
|
|||||||
private static Integer runningCashid = 0;
|
private static Integer runningCashid = 0;
|
||||||
|
|
||||||
private static void loadExistentCashIdsFromQuery(Connection con, String query) throws SQLException {
|
private static void loadExistentCashIdsFromQuery(Connection con, String query) throws SQLException {
|
||||||
PreparedStatement ps = con.prepareStatement(query);
|
try (PreparedStatement ps = con.prepareStatement(query);
|
||||||
ResultSet rs = ps.executeQuery();
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
int id = rs.getInt(1);
|
int id = rs.getInt(1);
|
||||||
@@ -46,30 +46,15 @@ public class MapleCashidGenerator {
|
|||||||
existentCashids.add(id);
|
existentCashids.add(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void loadExistentCashIdsFromDb() {
|
public static synchronized void loadExistentCashIdsFromDb() {
|
||||||
Connection con = null;
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
try {
|
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
|
|
||||||
loadExistentCashIdsFromQuery(con, "SELECT id FROM rings");
|
loadExistentCashIdsFromQuery(con, "SELECT id FROM rings");
|
||||||
loadExistentCashIdsFromQuery(con, "SELECT petid FROM pets");
|
loadExistentCashIdsFromQuery(con, "SELECT petid FROM pets");
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (con != null && !con.isClosed()) {
|
|
||||||
con.close();
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runningCashid = 0;
|
runningCashid = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user