Java SQL update statement is not updating in the table in spite of not getting errors












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?










share|improve this question




















  • 1





    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

    – Some programmer dude
    Jan 19 at 11:49






  • 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.

    – Some programmer dude
    Jan 19 at 11:50


















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?










share|improve this question




















  • 1





    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

    – Some programmer dude
    Jan 19 at 11:49






  • 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.

    – Some programmer dude
    Jan 19 at 11:50
















1












1








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?










share|improve this question
















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?







java mysql sql jdbc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 11:54









Mark Rotteveel

60.3k1477121




60.3k1477121










asked Jan 19 at 11:48









S KS K

155




155








  • 1





    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

    – Some programmer dude
    Jan 19 at 11:49






  • 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.

    – Some programmer dude
    Jan 19 at 11:50
















  • 1





    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

    – Some programmer dude
    Jan 19 at 11:49






  • 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.

    – Some programmer dude
    Jan 19 at 11:50










1




1





Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

– Some programmer dude
Jan 19 at 11:49





Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

– Some programmer dude
Jan 19 at 11:49




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.

– Some programmer dude
Jan 19 at 11:50







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.

– Some programmer dude
Jan 19 at 11:50














1 Answer
1






active

oldest

votes


















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();
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266751%2fjava-sql-update-statement-is-not-updating-in-the-table-in-spite-of-not-getting-e%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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();
    }
    }





    share|improve this answer






























      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();
      }
      }





      share|improve this answer




























        1












        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();
        }
        }





        share|improve this answer















        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();
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 19 at 13:45









        Mark Rotteveel

        60.3k1477121




        60.3k1477121










        answered Jan 19 at 11:53









        forpasforpas

        11.8k3424




        11.8k3424






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266751%2fjava-sql-update-statement-is-not-updating-in-the-table-in-spite-of-not-getting-e%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Callistus III

            Ostreoida

            Index Sanctorum