1
public void updateFields(BorrowedBook borrowedBook) throws SQLException {
    Integer copiesInBorrow = new Integer(0);
    Integer availableCopies = new Integer(0);
    PreparedStatement pstmt;
    try {
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM library_students.book WHERE BookID=" + "'" + getBookID(borrowedBook)+ "'");
        if((rs.next())){
            copiesInBorrow=rs.getInt(11);
            availableCopies=rs.getInt(13);
        }
        pstmt = con.prepareStatement("UPDATE library_students.book SET CopiesInBorrow= ? AND AvailableCopies=? WHERE BookID=?");
        pstmt.setInt(1,++copiesInBorrow);
        pstmt.setInt(2,--availableCopies);
        pstmt.setString(3,getBookID(borrowedBook));
        pstmt.executeUpdate();
        rs.close();
        }catch (SQLException ex) {
            ex.printStackTrace();
        }
}

I'm not getting any errors and the executeUpdate(); is returning also 1 what is the problem?

1
  • 1
    And please don't select unrelated tags. The C language is totally irrelevant to your problem. I also fail to see the relevance of the workbench tag. Therefore I have removed them. Commented Jan 19, 2019 at 11:50

1 Answer 1

1

You have a syntax error, change to:

UPDATE library_students.book SET CopiesInBorrow= ?, AvailableCopies=? WHERE BookID=?

Don't use AND between the columns you want to update. You could use AND in the WHERE part to apply the conditions you want.

If you wanted something like this:

UPDATE library_students.book SET CopiesInBorrow= ? WHERE AvailableCopies=? AND BookID=?

then the use of AND would be valid.

From your code I see that you just want to increase the value of a column and decrease another one. You don't need to find first these values in the table and then make the changes.

You can execute this statement:

UPDATE library_students.book SET CopiesInBorrow = CopiesInBorrow + 1, AvailableCopies = AvailableCopies - 1 WHERE BookID = ?

So change to this:

public void updateFields(BorrowedBook borrowedBook) throws SQLException {
    PreparedStatement pstmt;
    try {
        Statement stmt = con.createStatement();
        pstmt = con.prepareStatement("UPDATE library_students.book SET CopiesInBorrow = CopiesInBorrow + 1, AvailableCopies = AvailableCopies - 1 WHERE BookID = ?");
        pstmt.setString(1, getBookID(borrowedBook));
        pstmt.executeUpdate();
        rs.close();
    }catch (SQLException ex) {
        ex.printStackTrace();
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.