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