refactor: use try-with-resources for ring db operations
This commit is contained in:
@@ -21,13 +21,14 @@
|
|||||||
*/
|
*/
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
|
import client.inventory.manipulator.MapleCashidGenerator;
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
import tools.Pair;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import tools.Pair;
|
|
||||||
import tools.DatabaseConnection;
|
|
||||||
import client.inventory.manipulator.MapleCashidGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -50,18 +51,16 @@ public class MapleRing implements Comparable<MapleRing> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static MapleRing loadFromDb(int ringId) {
|
public static MapleRing loadFromDb(int ringId) {
|
||||||
try {
|
MapleRing ret = null;
|
||||||
MapleRing ret = null;
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
Connection con = DatabaseConnection.getConnection();
|
PreparedStatement ps = con.prepareStatement("SELECT * FROM rings WHERE id = ?")) {
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM rings WHERE id = ?"); // Get ring details..
|
|
||||||
ps.setInt(1, ringId);
|
ps.setInt(1, ringId);
|
||||||
ResultSet rs = ps.executeQuery();
|
|
||||||
if (rs.next()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
ret = new MapleRing(ringId, rs.getInt("partnerRingId"), rs.getInt("partnerChrId"), rs.getInt("itemid"), rs.getString("partnerName"));
|
if (rs.next()) {
|
||||||
|
ret = new MapleRing(ringId, rs.getInt("partnerRingId"), rs.getInt("partnerChrId"), rs.getInt("itemid"), rs.getString("partnerName"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
return ret;
|
return ret;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@@ -75,32 +74,30 @@ public class MapleRing implements Comparable<MapleRing> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM rings WHERE id=?")) {
|
||||||
|
ps.setInt(1, ring.getRingId());
|
||||||
|
ps.addBatch();
|
||||||
|
|
||||||
PreparedStatement ps = con.prepareStatement("DELETE FROM rings WHERE id=?");
|
ps.setInt(1, ring.getPartnerRingId());
|
||||||
ps.setInt(1, ring.getRingId());
|
ps.addBatch();
|
||||||
ps.addBatch();
|
|
||||||
|
|
||||||
ps.setInt(1, ring.getPartnerRingId());
|
ps.executeBatch();
|
||||||
ps.addBatch();
|
}
|
||||||
|
|
||||||
ps.executeBatch();
|
MapleCashidGenerator.freeCashId(ring.getRingId());
|
||||||
ps.close();
|
MapleCashidGenerator.freeCashId(ring.getPartnerRingId());
|
||||||
|
|
||||||
MapleCashidGenerator.freeCashId(ring.getRingId());
|
try (PreparedStatement ps = con.prepareStatement("UPDATE inventoryequipment SET ringid=-1 WHERE ringid=?")) {
|
||||||
MapleCashidGenerator.freeCashId(ring.getPartnerRingId());
|
ps.setInt(1, ring.getRingId());
|
||||||
|
ps.addBatch();
|
||||||
|
|
||||||
ps = con.prepareStatement("UPDATE inventoryequipment SET ringid=-1 WHERE ringid=?");
|
ps.setInt(1, ring.getPartnerRingId());
|
||||||
ps.setInt(1, ring.getRingId());
|
ps.addBatch();
|
||||||
ps.addBatch();
|
|
||||||
|
|
||||||
ps.setInt(1, ring.getPartnerRingId());
|
ps.executeBatch();
|
||||||
ps.addBatch();
|
}
|
||||||
|
}
|
||||||
ps.executeBatch();
|
|
||||||
ps.close();
|
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -118,24 +115,25 @@ public class MapleRing implements Comparable<MapleRing> {
|
|||||||
ringID[0] = MapleCashidGenerator.generateCashId();
|
ringID[0] = MapleCashidGenerator.generateCashId();
|
||||||
ringID[1] = MapleCashidGenerator.generateCashId();
|
ringID[1] = MapleCashidGenerator.generateCashId();
|
||||||
|
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
PreparedStatement ps = con.prepareStatement("INSERT INTO rings (id, itemid, partnerRingId, partnerChrId, partnername) VALUES (?, ?, ?, ?, ?)");
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO rings (id, itemid, partnerRingId, partnerChrId, partnername) VALUES (?, ?, ?, ?, ?)")) {
|
||||||
ps.setInt(1, ringID[0]);
|
ps.setInt(1, ringID[0]);
|
||||||
ps.setInt(2, itemid);
|
ps.setInt(2, itemid);
|
||||||
ps.setInt(3, ringID[1]);
|
ps.setInt(3, ringID[1]);
|
||||||
ps.setInt(4, partner2.getId());
|
ps.setInt(4, partner2.getId());
|
||||||
ps.setString(5, partner2.getName());
|
ps.setString(5, partner2.getName());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
ps.close();
|
}
|
||||||
ps = con.prepareStatement("INSERT INTO rings (id, itemid, partnerRingId, partnerChrId, partnername) VALUES (?, ?, ?, ?, ?)");
|
|
||||||
ps.setInt(1, ringID[1]);
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO rings (id, itemid, partnerRingId, partnerChrId, partnername) VALUES (?, ?, ?, ?, ?)")) {
|
||||||
ps.setInt(2, itemid);
|
ps.setInt(1, ringID[1]);
|
||||||
ps.setInt(3, ringID[0]);
|
ps.setInt(2, itemid);
|
||||||
ps.setInt(4, partner1.getId());
|
ps.setInt(3, ringID[0]);
|
||||||
ps.setString(5, partner1.getName());
|
ps.setInt(4, partner1.getId());
|
||||||
ps.executeUpdate();
|
ps.setString(5, partner1.getName());
|
||||||
ps.close();
|
ps.executeUpdate();
|
||||||
con.close();
|
}
|
||||||
|
}
|
||||||
return new Pair<>(ringID[0], ringID[1]);
|
return new Pair<>(ringID[0], ringID[1]);
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
|||||||
Reference in New Issue
Block a user