Hello.
I'm having a problem with my autodonation system. I'm using a static Connection but it seems to close it after some time for some odd reason.
Code:
private static boolean ONLINE = true;
private static Connection connection;
private static Statement statement;
public static void connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("creds would go here");
statement = connection.createStatement();
System.out.println("Connected to Webshop!");
ONLINE = true;
} catch(Exception e) {
e.printStackTrace();
ONLINE = false;
MySQL.reconnectAttempts++;
}
}
public static void claimPayment(final Player p) {
if(connection == null) {
if(MySQL.reconnectAttempts <= 7)
connect();
else
return;
}
if(!ONLINE) {
p.getPacketSender().sendMessage("The webshop is currently offline.");
return;
}
try {
String name2 = p.getUsername().replaceAll(" ", "_");
String query = "SELECT * FROM itemstore WHERE username = '"+name2+"'";
ResultSet rs = statement.executeQuery(query);
boolean claimed = false;
while(rs.next()) {
int prod = Integer.parseInt(rs.getString("productid"));
int price = Integer.parseInt(rs.getString("price"));
}
if (claimed) {
statement.execute("DELETE FROM `itemstore` WHERE `username` = '"+name2+"';");
p.getPacketSender().sendMessage("You have received the points or rank which you purchased.");
PlayerPanel.refreshPanel(p);
} else
p.getPacketSender().sendMessage("Your name was not found in our donation records.");
rs.close();
} catch (Exception e) {
e.printStackTrace();
p.getPacketSender().sendMessage("Unable to claim your item at this time. Try again soon.");
ONLINE = false;
}
}
Is it because I'm closing the ResultSet? Should I make it static and cache it, and whenever claimPayment is called it will update it and then not closing it?
I'm not experienced with these stuff that's why I need help
Would appreciate anything !