refactor: use try-with-resources for ban db operations
This commit is contained in:
@@ -23,9 +23,9 @@
|
|||||||
*/
|
*/
|
||||||
package client.command.commands.gm3;
|
package client.command.commands.gm3;
|
||||||
|
|
||||||
import client.command.Command;
|
|
||||||
import client.MapleClient;
|
|
||||||
import client.MapleCharacter;
|
import client.MapleCharacter;
|
||||||
|
import client.MapleClient;
|
||||||
|
import client.command.Command;
|
||||||
import net.server.Server;
|
import net.server.Server;
|
||||||
import server.TimerManager;
|
import server.TimerManager;
|
||||||
import tools.DatabaseConnection;
|
import tools.DatabaseConnection;
|
||||||
@@ -54,19 +54,15 @@ public class BanCommand extends Command {
|
|||||||
String readableTargetName = MapleCharacter.makeMapleReadable(target.getName());
|
String readableTargetName = MapleCharacter.makeMapleReadable(target.getName());
|
||||||
String ip = target.getClient().getSession().getRemoteAddress().toString().split(":")[0];
|
String ip = target.getClient().getSession().getRemoteAddress().toString().split(":")[0];
|
||||||
//Ban ip
|
//Ban ip
|
||||||
PreparedStatement ps = null;
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
try {
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
if (ip.matches("/[0-9]{1,3}\\..*")) {
|
if (ip.matches("/[0-9]{1,3}\\..*")) {
|
||||||
ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?, ?)");
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?, ?)")) {
|
||||||
ps.setString(1, ip);
|
ps.setString(1, ip);
|
||||||
ps.setString(2, String.valueOf(target.getClient().getAccID()));
|
ps.setString(2, String.valueOf(target.getClient().getAccID()));
|
||||||
|
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
ps.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
c.getPlayer().message("Error occured while banning IP address");
|
c.getPlayer().message("Error occured while banning IP address");
|
||||||
|
|||||||
@@ -23,9 +23,9 @@
|
|||||||
*/
|
*/
|
||||||
package client.command.commands.gm3;
|
package client.command.commands.gm3;
|
||||||
|
|
||||||
import client.command.Command;
|
|
||||||
import client.MapleClient;
|
|
||||||
import client.MapleCharacter;
|
import client.MapleCharacter;
|
||||||
|
import client.MapleClient;
|
||||||
|
import client.command.Command;
|
||||||
import tools.DatabaseConnection;
|
import tools.DatabaseConnection;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -44,20 +44,20 @@ public class UnBanCommand extends Command {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
int aid = MapleCharacter.getAccountIdByName(params[0]);
|
int aid = MapleCharacter.getAccountIdByName(params[0]);
|
||||||
|
|
||||||
PreparedStatement p = con.prepareStatement("UPDATE accounts SET banned = -1 WHERE id = " + aid);
|
try (PreparedStatement p = con.prepareStatement("UPDATE accounts SET banned = -1 WHERE id = " + aid)) {
|
||||||
p.executeUpdate();
|
p.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
p = con.prepareStatement("DELETE FROM ipbans WHERE aid = " + aid);
|
try (PreparedStatement p = con.prepareStatement("DELETE FROM ipbans WHERE aid = " + aid)) {
|
||||||
p.executeUpdate();
|
p.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
p = con.prepareStatement("DELETE FROM macbans WHERE aid = " + aid);
|
try (PreparedStatement p = con.prepareStatement("DELETE FROM macbans WHERE aid = " + aid)) {
|
||||||
p.executeUpdate();
|
p.executeUpdate();
|
||||||
|
}
|
||||||
con.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
player.message("Failed to unban " + params[0]);
|
player.message("Failed to unban " + params[0]);
|
||||||
|
|||||||
Reference in New Issue
Block a user