refactor: use try-with-resources for monster information db operations
This commit is contained in:
@@ -22,18 +22,6 @@ package server.life;
|
|||||||
|
|
||||||
import config.YamlConfig;
|
import config.YamlConfig;
|
||||||
import constants.inventory.ItemConstants;
|
import constants.inventory.ItemConstants;
|
||||||
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.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import provider.MapleData;
|
import provider.MapleData;
|
||||||
import provider.MapleDataProvider;
|
import provider.MapleDataProvider;
|
||||||
import provider.MapleDataProviderFactory;
|
import provider.MapleDataProviderFactory;
|
||||||
@@ -43,6 +31,13 @@ import tools.DatabaseConnection;
|
|||||||
import tools.Pair;
|
import tools.Pair;
|
||||||
import tools.Randomizer;
|
import tools.Randomizer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class MapleMonsterInformationProvider {
|
public class MapleMonsterInformationProvider {
|
||||||
// Author : LightPepsi
|
// Author : LightPepsi
|
||||||
|
|
||||||
@@ -92,45 +87,20 @@ public class MapleMonsterInformationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void retrieveGlobal() {
|
private void retrieveGlobal() {
|
||||||
PreparedStatement ps = null;
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
ResultSet rs = null;
|
PreparedStatement ps = con.prepareStatement("SELECT * FROM drop_data_global WHERE chance > 0");
|
||||||
Connection con = null;
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
try {
|
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
ps = con.prepareStatement("SELECT * FROM drop_data_global WHERE chance > 0");
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
globaldrops.add(
|
globaldrops.add(new MonsterGlobalDropEntry(
|
||||||
new MonsterGlobalDropEntry(
|
rs.getInt("itemid"),
|
||||||
rs.getInt("itemid"),
|
rs.getInt("chance"),
|
||||||
rs.getInt("chance"),
|
rs.getByte("continent"),
|
||||||
rs.getByte("continent"),
|
rs.getInt("minimum_quantity"),
|
||||||
rs.getInt("minimum_quantity"),
|
rs.getInt("maximum_quantity"),
|
||||||
rs.getInt("maximum_quantity"),
|
rs.getShort("questid")));
|
||||||
rs.getShort("questid")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error retrieving drop" + e);
|
System.err.println("Error retrieving drop" + e);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (ps != null && !ps.isClosed()) {
|
|
||||||
ps.close();
|
|
||||||
}
|
|
||||||
if (rs != null && !rs.isClosed()) {
|
|
||||||
rs.close();
|
|
||||||
}
|
|
||||||
if (con != null && !con.isClosed()) {
|
|
||||||
con.close();
|
|
||||||
}
|
|
||||||
} catch (SQLException ignore) {
|
|
||||||
ignore.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,39 +153,20 @@ public class MapleMonsterInformationProvider {
|
|||||||
}
|
}
|
||||||
final List<MonsterDropEntry> ret = new LinkedList<>();
|
final List<MonsterDropEntry> ret = new LinkedList<>();
|
||||||
|
|
||||||
PreparedStatement ps = null;
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
ResultSet rs = null;
|
PreparedStatement ps = con.prepareStatement("SELECT itemid, chance, minimum_quantity, maximum_quantity, questid FROM drop_data WHERE dropperid = ?")) {
|
||||||
Connection con = null;
|
|
||||||
try {
|
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
ps = con.prepareStatement("SELECT itemid, chance, minimum_quantity, maximum_quantity, questid FROM drop_data WHERE dropperid = ?");
|
|
||||||
ps.setInt(1, monsterId);
|
ps.setInt(1, monsterId);
|
||||||
rs = ps.executeQuery();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
ret.add(new MonsterDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getShort("questid")));
|
while (rs.next()) {
|
||||||
|
ret.add(new MonsterDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getShort("questid")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return ret;
|
return ret;
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (ps != null && !ps.isClosed()) {
|
|
||||||
ps.close();
|
|
||||||
}
|
|
||||||
if (rs != null && !rs.isClosed()) {
|
|
||||||
rs.close();
|
|
||||||
}
|
|
||||||
if (con != null && !con.isClosed()) {
|
|
||||||
con.close();
|
|
||||||
}
|
|
||||||
} catch (SQLException ignore) {
|
|
||||||
ignore.printStackTrace();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
drops.put(monsterId, ret);
|
drops.put(monsterId, ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user