执行 SELECT 查询时 PreparedStatement 和 ResultSet 出现问题?
Troubles with PreparedStatement and ResultSet when executing SELECT query?
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);
}
我只需要 1 个 ID 单元格即可对其进行操作,"SELECT balance
FROM visitor
WHERE cardID
=?"。如果有人能提出更简单的方法,我将不胜感激。
您忘记了对 next()
的调用,它将使光标前进到第一行(如果存在)。
A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
if (rs1.next())
{
int tempBonus=rs1.getInt(1);
}
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);
}
我只需要 1 个 ID 单元格即可对其进行操作,"SELECT balance
FROM visitor
WHERE cardID
=?"。如果有人能提出更简单的方法,我将不胜感激。
您忘记了对 next()
的调用,它将使光标前进到第一行(如果存在)。
A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
if (rs1.next())
{
int tempBonus=rs1.getInt(1);
}