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 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");

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]);