refactor: use try-with-resources for Duey db operations

This commit is contained in:
P0nk
2021-04-04 16:12:46 +02:00
parent 817a518e82
commit 0c267f8eee
2 changed files with 154 additions and 265 deletions

View File

@@ -21,60 +21,41 @@
*/
package net.server.channel.handlers;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import client.*;
import client.inventory.*;
import client.keybind.MapleKeyBinding;
import config.YamlConfig;
import constants.game.GameConstants;
import constants.game.ScriptableNPCConstants;
import net.AbstractMaplePacketHandler;
import net.server.PlayerBuffValueHolder;
import net.server.Server;
import net.server.channel.Channel;
import net.server.channel.CharacterIdChannelPair;
import net.server.coordinator.session.MapleSessionCoordinator;
import net.server.coordinator.world.MapleEventRecallCoordinator;
import net.server.guild.MapleAlliance;
import net.server.guild.MapleGuild;
import net.server.world.MaplePartyCharacter;
import net.server.world.PartyOperation;
import net.server.world.World;
import org.apache.mina.core.session.IoSession;
import scripting.event.EventInstanceManager;
import server.life.MobSkill;
import tools.DatabaseConnection;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import tools.Pair;
import tools.data.input.SeekableLittleEndianAccessor;
import client.BuddyList;
import client.BuddylistEntry;
import client.CharacterNameAndId;
import client.MapleCharacter;
import client.MapleClient;
import client.MapleDisease;
import client.MapleFamily;
import client.MapleFamilyEntry;
import client.keybind.MapleKeyBinding;
import client.MapleMount;
import client.SkillFactory;
import client.inventory.Equip;
import client.inventory.Item;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import client.inventory.MaplePet;
import constants.game.GameConstants;
import constants.game.ScriptableNPCConstants;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.server.coordinator.world.MapleEventRecallCoordinator;
import net.server.coordinator.session.MapleSessionCoordinator;
import org.apache.mina.core.session.IoSession;
import server.life.MobSkill;
import scripting.event.EventInstanceManager;
import tools.packets.Wedding;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
public final class PlayerLoggedinHandler extends AbstractMaplePacketHandler {
private static Set<Integer> attemptingLoginAccounts = new HashSet<>();
@@ -443,48 +424,22 @@ public final class PlayerLoggedinHandler extends AbstractMaplePacketHandler {
}
private static void showDueyNotification(MapleClient c, MapleCharacter player) {
Connection con = null;
PreparedStatement ps = null;
PreparedStatement pss = null;
ResultSet rs = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC");
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT Type FROM dueypackages WHERE ReceiverId = ? AND Checked = 1 ORDER BY Type DESC")) {
ps.setInt(1, player.getId());
rs = ps.executeQuery();
if (rs.next()) {
try {
Connection con2 = DatabaseConnection.getConnection();
pss = con2.prepareStatement("UPDATE dueypackages SET Checked = 0 WHERE ReceiverId = ?");
pss.setInt(1, player.getId());
pss.executeUpdate();
pss.close();
con2.close();
c.announce(MaplePacketCreator.sendDueyParcelNotification(rs.getInt("Type") == 1));
} catch (SQLException e) {
e.printStackTrace();
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
try (PreparedStatement ps2 = con.prepareStatement("UPDATE dueypackages SET Checked = 0 WHERE ReceiverId = ?")){
ps2.setInt(1, player.getId());
ps2.executeUpdate();
c.announce(MaplePacketCreator.sendDueyParcelNotification(rs.getInt("Type") == 1));
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pss != null) {
pss.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}