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

This commit is contained in:
P0nk
2021-04-05 00:11:42 +02:00
parent 429f82e4fe
commit e13c313839
5 changed files with 222 additions and 229 deletions

View File

@@ -21,16 +21,16 @@
*/
package net.server.channel.handlers;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import client.MapleClient;
import net.AbstractMaplePacketHandler;
import tools.DatabaseConnection;
import tools.MaplePacketCreator;
import tools.data.input.SeekableLittleEndianAccessor;
import client.MapleClient;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public final class NoteActionHandler extends AbstractMaplePacketHandler {
@Override
@@ -40,11 +40,12 @@ public final class NoteActionHandler extends AbstractMaplePacketHandler {
String charname = slea.readMapleAsciiString();
String message = slea.readMapleAsciiString();
try {
if (c.getPlayer().getCashShop().isOpened())
if (c.getPlayer().getCashShop().isOpened()) {
c.announce(MaplePacketCreator.showCashInventory(c));
c.getPlayer().sendNote(charname, message, (byte) 1);
c.getPlayer().getCashShop().decreaseNotes();
}
c.getPlayer().sendNote(charname, message, (byte) 1);
c.getPlayer().getCashShop().decreaseNotes();
} catch (SQLException e) {
e.printStackTrace();
}
@@ -56,21 +57,22 @@ public final class NoteActionHandler extends AbstractMaplePacketHandler {
for (int i = 0; i < num; i++) {
int id = slea.readInt();
slea.readByte(); //Fame, but we read it from the database :)
PreparedStatement ps;
try {
Connection con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `fame` FROM notes WHERE id=? AND deleted=0");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next())
fame += rs.getInt("fame");
rs.close();
ps = con.prepareStatement("UPDATE notes SET `deleted` = 1 WHERE id = ?");
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
con.close();
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT `fame` FROM notes WHERE id=? AND deleted=0")) {
ps.setInt(1, id);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
fame += rs.getInt("fame");
}
}
}
try (PreparedStatement ps = con.prepareStatement("UPDATE notes SET `deleted` = 1 WHERE id = ?")) {
ps.setInt(1, id);
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}