diff --git a/src/main/java/client/command/commands/gm3/BanCommand.java b/src/main/java/client/command/commands/gm3/BanCommand.java index cbc2d1087e..bfb3e55aaf 100644 --- a/src/main/java/client/command/commands/gm3/BanCommand.java +++ b/src/main/java/client/command/commands/gm3/BanCommand.java @@ -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 net.server.Server; import server.TimerManager; import tools.DatabaseConnection; @@ -54,19 +54,15 @@ public class BanCommand extends Command { String readableTargetName = MapleCharacter.makeMapleReadable(target.getName()); String ip = target.getClient().getSession().getRemoteAddress().toString().split(":")[0]; //Ban ip - PreparedStatement ps = null; - try { - Connection con = DatabaseConnection.getConnection(); + try (Connection con = DatabaseConnection.getConnection()) { if (ip.matches("/[0-9]{1,3}\\..*")) { - ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?, ?)"); - ps.setString(1, ip); - ps.setString(2, String.valueOf(target.getClient().getAccID())); + try (PreparedStatement ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?, ?)")) { + ps.setString(1, ip); + ps.setString(2, String.valueOf(target.getClient().getAccID())); - ps.executeUpdate(); - ps.close(); + ps.executeUpdate(); + } } - - con.close(); } catch (SQLException ex) { ex.printStackTrace(); c.getPlayer().message("Error occured while banning IP address"); diff --git a/src/main/java/client/command/commands/gm3/UnBanCommand.java b/src/main/java/client/command/commands/gm3/UnBanCommand.java index b859fef3dc..878e4fa2fd 100644 --- a/src/main/java/client/command/commands/gm3/UnBanCommand.java +++ b/src/main/java/client/command/commands/gm3/UnBanCommand.java @@ -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]);