refactor: use try-with-resources for db operations in various places
This commit is contained in:
@@ -19,23 +19,24 @@
|
||||
*/
|
||||
package server;
|
||||
|
||||
import client.MapleClient;
|
||||
import client.MapleCharacter;
|
||||
import client.MapleClient;
|
||||
import client.inventory.Item;
|
||||
import client.inventory.ItemFactory;
|
||||
import client.inventory.MapleInventory;
|
||||
import client.inventory.MapleInventoryType;
|
||||
import client.inventory.manipulator.MapleInventoryManipulator;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import scripting.event.EventManager;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.Pair;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import scripting.event.EventManager;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.Pair;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -118,10 +119,8 @@ public class MapleMarriage extends EventInstanceManager {
|
||||
public static boolean claimGiftItems(MapleClient c, MapleCharacter chr) {
|
||||
List<Item> gifts = loadGiftItemsFromDb(c, chr.getId());
|
||||
if (MapleInventory.checkSpot(chr, gifts)) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
ItemFactory.MARRIAGE_GIFTS.saveItems(new LinkedList<Pair<Item, MapleInventoryType>>(), chr.getId(), con);
|
||||
con.close();
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
ItemFactory.MARRIAGE_GIFTS.saveItems(new LinkedList<>(), chr.getId(), con);
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
@@ -159,11 +158,9 @@ public class MapleMarriage extends EventInstanceManager {
|
||||
for (Item it : giftItems) {
|
||||
items.add(new Pair<>(it, it.getInventoryType()));
|
||||
}
|
||||
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
ItemFactory.MARRIAGE_GIFTS.saveItems(items, cid, con);
|
||||
con.close();
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -19,22 +19,6 @@
|
||||
*/
|
||||
package server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import client.MapleCharacter;
|
||||
import provider.MapleData;
|
||||
import provider.MapleDataProvider;
|
||||
@@ -42,6 +26,16 @@ import provider.MapleDataProviderFactory;
|
||||
import provider.MapleDataTool;
|
||||
import tools.DatabaseConnection;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author RonanLana
|
||||
@@ -165,25 +159,18 @@ public class MapleSkillbookInformationProvider {
|
||||
}
|
||||
|
||||
private static void fetchSkillbooksFromReactors() {
|
||||
Connection con = null;
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps = con.prepareStatement("SELECT itemid FROM reactordrops WHERE itemid >= ? AND itemid < ?;");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT itemid FROM reactordrops WHERE itemid >= ? AND itemid < ?;")) {
|
||||
ps.setInt(1, skillbookMinItemid);
|
||||
ps.setInt(2, skillbookMaxItemid);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.isBeforeFirst()) {
|
||||
while(rs.next()) {
|
||||
foundSkillbooks.put(rs.getInt("itemid"), SkillBookEntry.REACTOR);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.isBeforeFirst()) {
|
||||
while (rs.next()) {
|
||||
foundSkillbooks.put(rs.getInt("itemid"), SkillBookEntry.REACTOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
con.close();
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -23,29 +23,24 @@ import client.inventory.Item;
|
||||
import client.inventory.ItemFactory;
|
||||
import client.inventory.MapleInventoryType;
|
||||
import constants.game.GameConstants;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
import provider.MapleData;
|
||||
import provider.MapleDataProvider;
|
||||
import provider.MapleDataProviderFactory;
|
||||
import provider.MapleDataTool;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.FilePrinter;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import provider.MapleData;
|
||||
import provider.MapleDataProvider;
|
||||
import provider.MapleDataProviderFactory;
|
||||
import provider.MapleDataTool;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.MaplePacketCreator;
|
||||
import tools.Pair;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import tools.FilePrinter;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -70,39 +65,34 @@ public class MapleStorage {
|
||||
}
|
||||
|
||||
private static MapleStorage create(int id, int world) throws SQLException {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, world, slots, meso) VALUES (?, ?, 4, 0)")) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, world, slots, meso) VALUES (?, ?, 4, 0)")) {
|
||||
ps.setInt(1, id);
|
||||
ps.setInt(2, world);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
con.close();
|
||||
|
||||
|
||||
return loadOrCreateFromDB(id, world);
|
||||
}
|
||||
|
||||
public static MapleStorage loadOrCreateFromDB(int id, int world) {
|
||||
try {
|
||||
MapleStorage ret;
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT storageid, slots, meso FROM storages WHERE accountid = ? AND world = ?");
|
||||
MapleStorage ret;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT storageid, slots, meso FROM storages WHERE accountid = ? AND world = ?")) {
|
||||
ps.setInt(1, id);
|
||||
ps.setInt(2, world);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
ret = new MapleStorage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso"));
|
||||
for (Pair<Item, MapleInventoryType> item : ItemFactory.STORAGE.loadItems(ret.id, false)) {
|
||||
ret.items.add(item.getLeft());
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
ret = new MapleStorage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso"));
|
||||
for (Pair<Item, MapleInventoryType> item : ItemFactory.STORAGE.loadItems(ret.id, false)) {
|
||||
ret.items.add(item.getLeft());
|
||||
}
|
||||
} else {
|
||||
ret = create(id, world);
|
||||
}
|
||||
} else {
|
||||
ret = create(id, world);
|
||||
}
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
|
||||
|
||||
return ret;
|
||||
} catch (SQLException ex) { // exceptions leading to deploy null storages found thanks to Jefe
|
||||
FilePrinter.printError(FilePrinter.STORAGE, ex, "SQL error occurred when trying to load storage for accountid " + id + ", world " + GameConstants.WORLD_NAMES[world]);
|
||||
|
||||
@@ -19,19 +19,15 @@
|
||||
*/
|
||||
package server.expeditions;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import config.YamlConfig;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.Pair;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Calendar;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Conrad
|
||||
@@ -123,18 +119,14 @@ public class MapleExpeditionBossLog {
|
||||
private static void resetBossLogTable(boolean week, Calendar c) {
|
||||
List<Pair<Timestamp, BossLogEntry>> resetTimestamps = BossLogEntry.getBossLogResetTimestamps(c, week);
|
||||
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
for (Pair<Timestamp, BossLogEntry> p : resetTimestamps) {
|
||||
PreparedStatement ps = con.prepareStatement("DELETE FROM " + getBossLogTable(week) + " WHERE attempttime <= ? AND bosstype LIKE ?");
|
||||
ps.setTimestamp(1, p.getLeft());
|
||||
ps.setString(2, p.getRight().name());
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM " + getBossLogTable(week) + " WHERE attempttime <= ? AND bosstype LIKE ?")) {
|
||||
ps.setTimestamp(1, p.getLeft());
|
||||
ps.setString(2, p.getRight().name());
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -146,21 +138,18 @@ public class MapleExpeditionBossLog {
|
||||
|
||||
private static int countPlayerEntries(int cid, BossLogEntry boss) {
|
||||
int ret_count = 0;
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps;
|
||||
ps = con.prepareStatement("SELECT COUNT(*) FROM " + getBossLogTable(boss.week) + " WHERE characterid = ? AND bosstype LIKE ?");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM " + getBossLogTable(boss.week) + " WHERE characterid = ? AND bosstype LIKE ?")) {
|
||||
ps.setInt(1, cid);
|
||||
ps.setString(2, boss.name());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
ret_count = rs.getInt(1);
|
||||
} else {
|
||||
ret_count = -1;
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
ret_count = rs.getInt(1);
|
||||
} else {
|
||||
ret_count = -1;
|
||||
}
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
return ret_count;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -169,14 +158,11 @@ public class MapleExpeditionBossLog {
|
||||
}
|
||||
|
||||
private static void insertPlayerEntry(int cid, BossLogEntry boss) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO " + getBossLogTable(boss.week) + " (characterid, bosstype) VALUES (?,?)");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO " + getBossLogTable(boss.week) + " (characterid, bosstype) VALUES (?,?)")) {
|
||||
ps.setInt(1, cid);
|
||||
ps.setString(2, boss.name());
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -21,8 +21,17 @@
|
||||
*/
|
||||
package server.maps;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import provider.MapleData;
|
||||
import provider.MapleDataProvider;
|
||||
import provider.MapleDataProviderFactory;
|
||||
import provider.MapleDataTool;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import server.life.*;
|
||||
import server.partyquest.GuardianSpawnPoint;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.StringUtil;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -31,19 +40,6 @@ import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import provider.MapleData;
|
||||
import provider.MapleDataProvider;
|
||||
import provider.MapleDataProviderFactory;
|
||||
import provider.MapleDataTool;
|
||||
import server.life.AbstractLoadedMapleLife;
|
||||
import server.life.MapleLifeFactory;
|
||||
import server.life.MapleMonster;
|
||||
import server.life.MaplePlayerNPC;
|
||||
import server.life.MaplePlayerNPCFactory;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import server.partyquest.GuardianSpawnPoint;
|
||||
import tools.DatabaseConnection;
|
||||
import tools.StringUtil;
|
||||
|
||||
public class MapleMapFactory {
|
||||
|
||||
@@ -84,33 +80,29 @@ public class MapleMapFactory {
|
||||
}
|
||||
|
||||
private static void loadLifeFromDb(MapleMap map) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM plife WHERE map = ? and world = ?");
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM plife WHERE map = ? and world = ?")) {
|
||||
ps.setInt(1, map.getId());
|
||||
ps.setInt(2, map.getWorld());
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("life");
|
||||
String type = rs.getString("type");
|
||||
int cy = rs.getInt("cy");
|
||||
int f = rs.getInt("f");
|
||||
int fh = rs.getInt("fh");
|
||||
int rx0 = rs.getInt("rx0");
|
||||
int rx1 = rs.getInt("rx1");
|
||||
int x = rs.getInt("x");
|
||||
int y = rs.getInt("y");
|
||||
int hide = rs.getInt("hide");
|
||||
int mobTime = rs.getInt("mobtime");
|
||||
int team = rs.getInt("team");
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("life");
|
||||
String type = rs.getString("type");
|
||||
int cy = rs.getInt("cy");
|
||||
int f = rs.getInt("f");
|
||||
int fh = rs.getInt("fh");
|
||||
int rx0 = rs.getInt("rx0");
|
||||
int rx1 = rs.getInt("rx1");
|
||||
int x = rs.getInt("x");
|
||||
int y = rs.getInt("y");
|
||||
int hide = rs.getInt("hide");
|
||||
int mobTime = rs.getInt("mobtime");
|
||||
int team = rs.getInt("team");
|
||||
|
||||
loadLifeRaw(map, id, type, cy, f, fh, rx0, rx1, x, y, hide, mobTime, team);
|
||||
loadLifeRaw(map, id, type, cy, f, fh, rx0, rx1, x, y, hide, mobTime, team);
|
||||
}
|
||||
}
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
con.close();
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
@@ -243,18 +235,16 @@ public class MapleMapFactory {
|
||||
map.setSeats(seats);
|
||||
}
|
||||
if (event == null) {
|
||||
try {
|
||||
Connection con = DatabaseConnection.getConnection();
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs WHERE map = ? AND world = ?")) {
|
||||
ps.setInt(1, mapid);
|
||||
ps.setInt(2, world);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
map.addPlayerNPCMapObject(new MaplePlayerNPC(rs));
|
||||
}
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs WHERE map = ? AND world = ?")) {
|
||||
ps.setInt(1, mapid);
|
||||
ps.setInt(2, world);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
map.addPlayerNPCMapObject(new MaplePlayerNPC(rs));
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user