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

@@ -24,8 +24,8 @@
package client.command.commands.gm1;
import client.MapleCharacter;
import client.command.Command;
import client.MapleClient;
import client.command.Command;
import server.MapleItemInformationProvider;
import server.life.MapleMonsterInformationProvider;
import tools.DatabaseConnection;
@@ -48,31 +48,29 @@ public class WhoDropsCommand extends Command {
player.dropMessage(5, "Please do @whodrops <item name>");
return;
}
if (c.tryacquireClient()) {
try {
String searchString = player.getLastCommandMessage();
String output = "";
Iterator<Pair<Integer, String>> listIterator = MapleItemInformationProvider.getInstance().getItemDataByName(searchString).iterator();
if(listIterator.hasNext()) {
if (listIterator.hasNext()) {
int count = 1;
while(listIterator.hasNext() && count <= 3) {
while (listIterator.hasNext() && count <= 3) {
Pair<Integer, String> data = listIterator.next();
output += "#b" + data.getRight() + "#k is dropped by:\r\n";
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT dropperid FROM drop_data WHERE itemid = ? LIMIT 50");
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT dropperid FROM drop_data WHERE itemid = ? LIMIT 50")) {
ps.setInt(1, data.getLeft());
ResultSet rs = ps.executeQuery();
while(rs.next()) {
String resultName = MapleMonsterInformationProvider.getInstance().getMobNameFromId(rs.getInt("dropperid"));
if (resultName != null) {
output += resultName + ", ";
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String resultName = MapleMonsterInformationProvider.getInstance().getMobNameFromId(rs.getInt("dropperid"));
if (resultName != null) {
output += resultName + ", ";
}
}
}
rs.close();
ps.close();
con.close();
} catch (Exception e) {
player.dropMessage(6, "There was a problem retrieving the required data. Please try again.");
e.printStackTrace();
@@ -85,7 +83,7 @@ public class WhoDropsCommand extends Command {
player.dropMessage(5, "The item you searched for doesn't exist.");
return;
}
c.getAbstractPlayerInteraction().npcTalk(9010000, output);
} finally {
c.releaseClient();