UPDATE和INSERT查询没有问题,只有SELECT有问题,这里所有代码:
try {
String nCard = jTextField1.getText();
String deprecate = jTextField2.getText();
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url = "jdbc:mysql://localhost:3306/visits";
Connection conn1 = DriverManager.getConnection(url, "root", "");
PreparedStatement pstmt = conn1.prepareStatement("INSERT INTO `visits`.`transaction` (`numbercard`, `deprecate`) VALUES (?, ?)");
pstmt.setString(1, nCard);
pstmt.setString(2, deprecate);
pstmt.executeUpdate();
DrawTable();
PreparedStatement pstmt1 = conn1.prepareStatement("SELECT `balance` FROM `visitor` WHERE `cardID`=?");
int nCardInt = Integer.parseInt(nCard);
pstmt1.setInt(1, nCardInt);
ResultSet rs1 = pstmt1.executeQuery();
int tempBonus=rs1.getInt(1);
tempBonus-=Integer.parseInt(deprecate);
String bonusString = String.valueOf(tempBonus);
PreparedStatement pstmt2 = conn1.prepareStatement("UPDATE `visitor` SET `balance`=? WHERE cardID=?");
pstmt2.setString(1, bonusString);
pstmt2.setString(2, nCard);
pstmt2.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
我只需要一个ID为"SELECT balance
FROM visitor
WHERE cardID
=?"的单元格即可进行操作。如果有人能提出更简单的方法,我将不胜感激。
您忘记了对next()
的调用,该调用将使光标前进到第一行(如果存在)。
ResultSet光标最初位于第一行之前;对该方法的第一次调用接下来使第一行成为当前行;第二个调用使第二行成为当前行,依此类推
if (rs1.next())
{
int tempBonus=rs1.getInt(1);
}