refactor: use try-with-resources for db operations in various places

This commit is contained in:
P0nk
2021-04-05 00:19:59 +02:00
parent 4ec4600406
commit 69635f5e6c
10 changed files with 171 additions and 267 deletions

View File

@@ -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();
}