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

This commit is contained in:
P0nk
2021-04-04 23:59:24 +02:00
parent 8b686b60f1
commit 80e193398c
2 changed files with 20 additions and 24 deletions

View File

@@ -23,9 +23,9 @@
*/
package client.command.commands.gm3;
import client.command.Command;
import client.MapleClient;
import client.MapleCharacter;
import client.MapleClient;
import client.command.Command;
import tools.DatabaseConnection;
import java.sql.Connection;
@@ -44,20 +44,20 @@ public class UnBanCommand extends Command {
return;
}
try {
Connection con = DatabaseConnection.getConnection();
try (Connection con = DatabaseConnection.getConnection()) {
int aid = MapleCharacter.getAccountIdByName(params[0]);
PreparedStatement p = con.prepareStatement("UPDATE accounts SET banned = -1 WHERE id = " + aid);
p.executeUpdate();
try (PreparedStatement p = con.prepareStatement("UPDATE accounts SET banned = -1 WHERE id = " + aid)) {
p.executeUpdate();
}
p = con.prepareStatement("DELETE FROM ipbans WHERE aid = " + aid);
p.executeUpdate();
try (PreparedStatement p = con.prepareStatement("DELETE FROM ipbans WHERE aid = " + aid)) {
p.executeUpdate();
}
p = con.prepareStatement("DELETE FROM macbans WHERE aid = " + aid);
p.executeUpdate();
con.close();
try (PreparedStatement p = con.prepareStatement("DELETE FROM macbans WHERE aid = " + aid)) {
p.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
player.message("Failed to unban " + params[0]);