我在Java应用程序中获取SELECT查询时遇到问题,无法返回正确的INT值。看起来它只返回INT为1,当它应该获取列中的INT时,如MySQL查询浏览器中运行的查询所示:
以下是导致问题的代码块。特别是“val =(rs.getInt(”quantity“));”
附近的行想知道我是否遗漏了一些完全明显的东西。以下是该方法的代码块的一部分:
try {
IItem Item = (IItem) pItem.getItem().copy();
int ItemQuantity = pItem.getItem().getQuantity();
int ItemId = pItem.getItem().getItemId();
ResultSet rs = null;
PreparedStatement ps;
ps = DatabaseConnection.getConnection().prepareStatement("SELECT quantity FROM hiredmerchant WHERE ownerid = ? AND itemid = ? AND quantity = ?");
ps.setInt(1, shopowner);
ps.setInt(2, ItemId);
ps.setInt(3, ItemQuantity);
rs = ps.executeQuery();
while (rs.next()) {
int val;
val = (rs.getInt("quantity"));
System.out.println("Selected quantity of " + val + " from " + this.getOwnerName() + "'s merchant items: ");
if (ItemQuantity == val) {
try {
PreparedStatement os;
os = DatabaseConnection.getConnection().prepareStatement("DELETE FROM hiredmerchant WHERE ownerid = ? AND itemid = ? AND quantity = ?");
os.setInt(1, shopowner);
os.setInt(2, ItemId);
os.setInt(3, ItemQuantity);
os.executeUpdate();
os.close();
} catch (SQLException ex) {
System.out.println("Error deleting quantity row from " + this.getOwnerName() + "'s merchant items: " + ex);
}
} else {
try {
PreparedStatement os;
os = DatabaseConnection.getConnection().prepareStatement("UPDATE hiredmerchant SET quantity = quanity-(" + quantity + ") WHERE ownerid = ? AND itemid = ? AND quantity = ?");
os.setInt(1, shopowner);
os.setInt(2, ItemId);
os.setInt(3, ItemQuantity);
os.executeUpdate();
os.close();
} catch (SQLException ex) {
System.out.println("Error deducting quantity from row of " + this.getOwnerName() + "'s merchant items: " + ex);
}
}
ps.close();
}
} catch (SQLException ex) {
System.out.println("Error grabbing item quantity from " + this.getOwnerName() + "'s merchant items: " + ex);
}
如果我需要提供整个方法或更多信息,请告诉我。