There are no problems with UPDATE and INSERT query, troubles only with SELECT, here all code:
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);
}
I need only 1 cell by ID to manipulate with it, "SELECT balance
FROM visitor
WHERE cardID
=?". If anyone will suggest easyer methods, I would be very grateful.
You forgot the call to next()
, which will advance the cursor to the first row, if present.
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);
}