测试两个 result.next()
Testing two result.next()
我需要帮助才能知道为什么我准备好的语句之一没有执行。这是代码:
准备好的语句:
updaterBalance = connection.prepareStatement("UPDATE accounts SET balance = ? WHERE account_id = ?")
table :
statmnt.executeUpdate("CREATE TABLE accounts ( account_id INTEGER, balance DOUBLE PRECISION)");
statmnt.executeUpdate("ALTER TABLE accounts ADD CONSTRAINT balance_must_be_positive CHECK (balance >= 0)");
失败的方法:
public boolean transfer(int from, int to, double amount) throws DataStoreException {
try {
lookForAccount.setInt(1, to);
ResultSet result = lookForAccount.executeQuery();
if(result.next()) {
result.close();
lookForAccount.setInt(1, from);
ResultSet result2 = lookForAccount.executeQuery();
if(result2.next()) {
updaterBalance.setDouble(1, result2.getDouble("balance") - amount);
updaterBalance.setInt(2, from);
updaterBalance.executeUpdate();
result2.close();
updaterBalance.setDouble(1, result2.getDouble("balance") + amount);
updaterBalance.setInt(2, to);
updaterBalance.executeUpdate();
return true;
}
else {
return false;
}
}
else {
return false;
}
} catch (Exception error) {
error.printStackTrace();
return false;
}
}
来电:
boolean a = manager.transfer(1, 2, 10);
准备好的语句工作得很好是一个 addMoney 方法,它可以在单个指定的 account_id 上添加钱。在调用时,第一个 account_id 的余额为 1000,第二个为 0。
我从来没有进入过 if 块。我根本无法满足条件。
有什么想法吗?
documentation for Statement
说:
By default, only one ResultSet
object per Statement
object can be open
at the same time. Therefore, if the reading of one ResultSet
object is
interleaved with the reading of another, each must have been generated
by different Statement
objects. All execution methods in the Statement
interface implicitly close a statment's current ResultSet
object if an
open one exists.
这基本上意味着因为您对 result
和 result2
使用了相同的 lookForAccount
语句,所以 result
将被关闭,因此 result.next()
将不是 return true
.
我需要帮助才能知道为什么我准备好的语句之一没有执行。这是代码:
准备好的语句:
updaterBalance = connection.prepareStatement("UPDATE accounts SET balance = ? WHERE account_id = ?")
table :
statmnt.executeUpdate("CREATE TABLE accounts ( account_id INTEGER, balance DOUBLE PRECISION)");
statmnt.executeUpdate("ALTER TABLE accounts ADD CONSTRAINT balance_must_be_positive CHECK (balance >= 0)");
失败的方法:
public boolean transfer(int from, int to, double amount) throws DataStoreException {
try {
lookForAccount.setInt(1, to);
ResultSet result = lookForAccount.executeQuery();
if(result.next()) {
result.close();
lookForAccount.setInt(1, from);
ResultSet result2 = lookForAccount.executeQuery();
if(result2.next()) {
updaterBalance.setDouble(1, result2.getDouble("balance") - amount);
updaterBalance.setInt(2, from);
updaterBalance.executeUpdate();
result2.close();
updaterBalance.setDouble(1, result2.getDouble("balance") + amount);
updaterBalance.setInt(2, to);
updaterBalance.executeUpdate();
return true;
}
else {
return false;
}
}
else {
return false;
}
} catch (Exception error) {
error.printStackTrace();
return false;
}
}
来电:
boolean a = manager.transfer(1, 2, 10);
准备好的语句工作得很好是一个 addMoney 方法,它可以在单个指定的 account_id 上添加钱。在调用时,第一个 account_id 的余额为 1000,第二个为 0。 我从来没有进入过 if 块。我根本无法满足条件。
有什么想法吗?
documentation for Statement
说:
By default, only one
ResultSet
object perStatement
object can be open at the same time. Therefore, if the reading of oneResultSet
object is interleaved with the reading of another, each must have been generated by differentStatement
objects. All execution methods in theStatement
interface implicitly close a statment's currentResultSet
object if an open one exists.
这基本上意味着因为您对 result
和 result2
使用了相同的 lookForAccount
语句,所以 result
将被关闭,因此 result.next()
将不是 return true
.