我不是最擅长 SQL,我购买了这个脚本用于投票。直到昨晚,它就像一个魅力,在 ResultSet 对我关闭后它开始抛出 Operation not allowed 。程序员说它看起来不错,他也被这个错误弄糊涂了。
这是堆栈跟踪:
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7077)
at com.mysql.jdbc.UpdatableResultSet.next(UpdatableResultSet.java:1118)
at mysql.impl.FoxVote.run(FoxVote.java:43)
at java.lang.Thread.run(Unknown Source)
这是Java类:
public class FoxVote implements Runnable {
public static final String HOST = "misthalinpk.com";
public static final String USER = "deleted";
public static final String PASS = "deleted";
public static final String DATABASE = "deleted";
private Player player;
private Connection conn;
private Statement stmt;
private int votes;
public FoxVote(Player player) {
this.player = player;
}
@Override
public void run() {
try {
if (!connect(HOST, DATABASE, USER, PASS)) {
return;
}
String name = player.getUsername().replace(" ", "_");
ResultSet rs = executeQuery("SELECT * FROM fx_votes WHERE username='"+name+"' AND claimed=0 AND callback_date IS NOT NULL");
while (rs.next()) {
String timestamp = rs.getTimestamp("callback_date").toString();
String ipAddress = rs.getString("ip_address");
int siteId = rs.getInt("site_id");
if (player != null && player.getSession().getState() == SessionState.LOGGED_IN) {
player.getInventory().add(995, 100000);
player.getInventory().add(10944, 1);
votes++;
Achievements.finishAchievement(player, AchievementData.VOTE_FOR_US);
Achievements.doProgress(player, AchievementData.VOTE_100_TIMES);
if (++votes >= 15) {
World.sendMessage("[@red@Voting@bla@]@blu@Another 20 votes have been rewarded thanks to "+player.getUsername()+"!");
votes = 0;
System.out.println("[Voting] Vote claimed by "+name+". (sid: "+siteId+", ip: "+ipAddress+", time: "+timestamp+")");
rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
rs.updateRow();
}
destroy();
}}} catch (Exception e) {
e.printStackTrace();
}
}
public boolean connect(String host, String database, String user, String pass) {
try {
this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
return true;
} catch (SQLException e) {
System.out.println("Failing connecting to database!");
return false;
}
}
public void destroy() {
try {
conn.close();
conn = null;
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch(Exception e) {
e.printStackTrace();
}
}
public int executeUpdate(String query) {
try {
this.stmt = this.conn.createStatement(1005, 1008);
int results = stmt.executeUpdate(query);
return results;
} catch (SQLException ex) {
ex.printStackTrace();
}
return -1;
}
public ResultSet executeQuery(String query) {
try {
this.stmt = this.conn.createStatement(1005, 1008);
ResultSet results = stmt.executeQuery(query);
return results;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
此外,它应该一次给出 8 票(最多),但在抛出错误之前它只给出 1 票。
任何帮助都会很棒,谢谢。