refactor: use try-with-resources for Fredrick db operations

This commit is contained in:
P0nk
2021-04-05 00:13:32 +02:00
parent e13c313839
commit 4ec4600406

View File

@@ -29,15 +29,7 @@ import client.inventory.Item;
import client.inventory.ItemFactory;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import client.inventory.manipulator.MapleInventoryManipulator;
import java.util.Collections;
import net.server.Server;
import net.server.world.World;
import server.MapleItemInformationProvider;
@@ -47,6 +39,11 @@ import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.Pair;
import java.sql.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author RonanLana - synchronization of Fredrick modules and operation results
@@ -100,10 +97,8 @@ public class FredrickProcessor {
}
public static void removeFredrickLog(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
try (Connection con = DatabaseConnection.getConnection()) {
removeFredrickLog(con, cid);
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
@@ -112,22 +107,19 @@ public class FredrickProcessor {
private static void removeFredrickLog(Connection con, int cid) throws SQLException {
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `fredstorage` WHERE `cid` = ?")) {
ps.setInt(1, cid);
ps.execute();
ps.executeUpdate();
}
}
public static void insertFredrickLog(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
try (Connection con = DatabaseConnection.getConnection()) {
removeFredrickLog(con, cid);
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `fredstorage` (`cid`, `daynotes`, `timestamp`) VALUES (?, 0, ?)")) {
ps.setInt(1, cid);
ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
ps.execute();
ps.executeUpdate();
}
con.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
@@ -146,30 +138,25 @@ public class FredrickProcessor {
}
}
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `notes` WHERE `from` LIKE ? AND `to` LIKE ?")) {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM `notes` WHERE `from` LIKE ? AND `to` LIKE ?")) {
ps.setString(1, "FREDRICK");
for (String cname : expiredCnames) {
ps.setString(2, cname);
ps.executeBatch();
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void runFredrickSchedule() {
try {
Connection con = DatabaseConnection.getConnection();
try (Connection con = DatabaseConnection.getConnection()) {
List<Pair<Integer, Integer>> expiredCids = new LinkedList<>();
List<Pair<Pair<Integer, String>, Integer>> notifCids = new LinkedList<>();
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM fredstorage f LEFT JOIN (SELECT id, name, world, lastLogoutTime FROM characters) AS c ON c.id = f.cid")) {
try (ResultSet rs = ps.executeQuery()) {
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM fredstorage f LEFT JOIN (SELECT id, name, world, lastLogoutTime FROM characters) AS c ON c.id = f.cid");
ResultSet rs = ps.executeQuery()) {
long curTime = System.currentTimeMillis();
while (rs.next()) {
@@ -200,7 +187,7 @@ public class FredrickProcessor {
}
}
}
}
}
if (!expiredCids.isEmpty()) {
@@ -258,22 +245,18 @@ public class FredrickProcessor {
ps.executeBatch();
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static boolean deleteFredrickItems(int cid) {
try {
Connection con = DatabaseConnection.getConnection();
try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM `inventoryitems` WHERE `type` = ? AND `characterid` = ?")) {
ps.setInt(1, ItemFactory.MERCHANT.getValue());
ps.setInt(2, cid);
ps.execute();
}
con.close();
ps.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();