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