我有一个必须阻止用户的功能,并且在其中我检查是否有任何结果,但是,即使我设置为返回,它仍会以某种方式在while循环中返回Results are not available
如果没有可用的结果,从功能。处理ResultSet
时我做错了吗?这种检查没有结果的方法在所有其他功能中都运行良好,但这里存在问题。
public static boolean block(int id, int blocktime) throws Exception, SQLException {
DatabaseConnection db = new DatabaseConnection();
db.establishConnection().prepareStatement("SELECT * FROM accounts WHERE id = ?");
db.setIntParameter(1, id);
db.executeQuery();
if (!db.results().isBeforeFirst()) {
Notify.accountIdInvalid();
db.close();
return false;
}
while (db.results().next()) {
int blocked = db.results().getInt("blocked");
if (blocked == 1) { db.close(); Notify.accountAlreadyBlocked(); return false; }
else {
db.close();
DatabaseConnection db2 = new DatabaseConnection();
db2.establishConnection().prepareStatement("UPDATE accounts SET blocked = 1, blocktime = ? WHERE id = ?");
db2.setIntParameter(1, blocktime);
db2.setIntParameter(2, id);
db2.executeUpdate();
db2.close();
}
}
return true;
}
以下是DatabaseConnection
中用于处理语句和结果的一些函数。进程始终是相同的,我建立连接,准备语句并执行查询(或更新),它返回我使用的结果。
/**
* Executes the query.
*
* @return DatabaseConnection
* @throws Exception if statement doesn't exists.
*/
public DatabaseConnection executeQuery() throws Exception {
if (this.stmt == null) {
throw new Exception("Statement for query executing doesn't exist.");
}
this.rs = this.stmt.executeQuery();
return this;
}
/**
* Executes the update.
*
* @throws Exception
*/
public void executeUpdate() throws Exception {
if (this.stmt == null) {
throw new Exception("Statement for query updating doesn't exist.");
}
this.stmt.executeUpdate();
}
/**
* Returns the results from executed query.
*
* @return ResultSet
* @throws Exception if results aren't available.
*/
public ResultSet results() throws Exception {
if (this.rs == null) {
throw new Exception("Results are not available.");
}
return this.rs;
}
我认为它不相关,但无论如何;我正在使用MySQL数据库。