SQL not Updating Data












0















I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?



public void replaceData(ActionEvent event) {

try {

Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();

ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");


this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());

} catch (SQLException ex) {
ex.printStackTrace();
}

}









share|improve this question

























  • probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

    – kleopatra
    Nov 24 '18 at 14:38











  • It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

    – fabian
    Nov 24 '18 at 15:24
















0















I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?



public void replaceData(ActionEvent event) {

try {

Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();

ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");


this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());

} catch (SQLException ex) {
ex.printStackTrace();
}

}









share|improve this question

























  • probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

    – kleopatra
    Nov 24 '18 at 14:38











  • It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

    – fabian
    Nov 24 '18 at 15:24














0












0








0








I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?



public void replaceData(ActionEvent event) {

try {

Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();

ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");


this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());

} catch (SQLException ex) {
ex.printStackTrace();
}

}









share|improve this question
















I'm trying to update data in table in JavaFX. For some reason it wouldn't work. Anyone has an idea what could be wrong in the following code?



public void replaceData(ActionEvent event) {

try {

Connection conn = SqliteConnection.Connector();
this.data = FXCollections.observableArrayList();

ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName='" + ProductName1.getText() + "', Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock='" + TotalStock1.getText() + "' Where ID=" + ID2.getText() + "");


this.dataid.setText(ProductName1.getText());
this.dataBarcode.setText(Barcode1.getText());
this.dataCategory.setText(Category1.getText());
this.dataSubCategory.setText(SubCategory1.getText());
this.dataTotalStock.setText(TotalStock1.getText());
this.dataSinglePrice.setText(SinglePrice1.getText());

} catch (SQLException ex) {
ex.printStackTrace();
}

}






java sql jdbc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 15:52









a_horse_with_no_name

301k46459552




301k46459552










asked Nov 24 '18 at 14:09









Sektor InspektorSektor Inspektor

42




42













  • probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

    – kleopatra
    Nov 24 '18 at 14:38











  • It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

    – fabian
    Nov 24 '18 at 15:24



















  • probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

    – kleopatra
    Nov 24 '18 at 14:38











  • It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

    – fabian
    Nov 24 '18 at 15:24

















probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

– kleopatra
Nov 24 '18 at 14:38





probably something wrong in the code you are not showing :) Please provide a Minimal, Complete, and Verifiable example that demonstrates the problem. And unrelated to your problem: please learn java naming conventions and stick to them

– kleopatra
Nov 24 '18 at 14:38













It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

– fabian
Nov 24 '18 at 15:24





It's better to use a PreparedStatement for creating a query containing data you need to insert based on the gui input. Furthermore by leaving the catch empty, you ignore information that could be valuable for debugging. NEVER do this, unless the exception is an expected result. (At least put ex.printStackTrace(); in there.) There are other issues though: There is a , just before WHERE and you use executeQuery for a update query.

– fabian
Nov 24 '18 at 15:24












2 Answers
2






active

oldest

votes


















0














You did mistake in query statement you put comma before where close. Just delete that comma



ResultSet rs = conn.createStatement().executeQuery(
"UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");


paste this code






share|improve this answer


























  • This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

    – fabian
    Nov 24 '18 at 15:26





















0














At least two issues here:




  1. UPDATE statements do not return the updated row (would be nice if they did...) So you cannot expect to get a ResultSet from an update statement. Had you been using executeUpdate() instead of executeQuery() this would have been easier to spot.



  2. Instead of quoting the parameters when forming the SQL string, it is better to use a PreparedStatement. I.e., use something like:



    String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
    try(Connection conn = SqliteConnection.Connector();
    PreparedStatement stmt = conn.prepareStatement(sql)) {
    int i = 0;
    stmt.setString(++i, ProductName1.getText());
    ..
    stmt.setString(++i, ID2.getText());

    stmt.executeUpdate();
    }


    The above also ensures that the connection is properly closed.








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%2f53458994%2fsql-not-updating-data%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You did mistake in query statement you put comma before where close. Just delete that comma



    ResultSet rs = conn.createStatement().executeQuery(
    "UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");


    paste this code






    share|improve this answer


























    • This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

      – fabian
      Nov 24 '18 at 15:26


















    0














    You did mistake in query statement you put comma before where close. Just delete that comma



    ResultSet rs = conn.createStatement().executeQuery(
    "UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");


    paste this code






    share|improve this answer


























    • This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

      – fabian
      Nov 24 '18 at 15:26
















    0












    0








    0







    You did mistake in query statement you put comma before where close. Just delete that comma



    ResultSet rs = conn.createStatement().executeQuery(
    "UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");


    paste this code






    share|improve this answer















    You did mistake in query statement you put comma before where close. Just delete that comma



    ResultSet rs = conn.createStatement().executeQuery(
    "UPDATE "+SupermarketDB+" SET ProductName=" + ProductName1.getText() + ", Barcode=" + Barcode1.getText() + ", Category=" + Category1.getText() + ", SubCategory=" + SubCategory1.getText() + ", TotalStock=" + TotalStock1.getText() + " Where ID=" + ID2.getText() + "");


    paste this code







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 24 '18 at 15:42

























    answered Nov 24 '18 at 14:46









    Varun_AmbhireVarun_Ambhire

    93




    93













    • This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

      – fabian
      Nov 24 '18 at 15:26





















    • This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

      – fabian
      Nov 24 '18 at 15:26



















    This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

    – fabian
    Nov 24 '18 at 15:26







    This won't work either, even if the TextFields do not contain quotes... This is an update query, not a query returning a ResultSet.

    – fabian
    Nov 24 '18 at 15:26















    0














    At least two issues here:




    1. UPDATE statements do not return the updated row (would be nice if they did...) So you cannot expect to get a ResultSet from an update statement. Had you been using executeUpdate() instead of executeQuery() this would have been easier to spot.



    2. Instead of quoting the parameters when forming the SQL string, it is better to use a PreparedStatement. I.e., use something like:



      String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
      try(Connection conn = SqliteConnection.Connector();
      PreparedStatement stmt = conn.prepareStatement(sql)) {
      int i = 0;
      stmt.setString(++i, ProductName1.getText());
      ..
      stmt.setString(++i, ID2.getText());

      stmt.executeUpdate();
      }


      The above also ensures that the connection is properly closed.








    share|improve this answer




























      0














      At least two issues here:




      1. UPDATE statements do not return the updated row (would be nice if they did...) So you cannot expect to get a ResultSet from an update statement. Had you been using executeUpdate() instead of executeQuery() this would have been easier to spot.



      2. Instead of quoting the parameters when forming the SQL string, it is better to use a PreparedStatement. I.e., use something like:



        String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
        try(Connection conn = SqliteConnection.Connector();
        PreparedStatement stmt = conn.prepareStatement(sql)) {
        int i = 0;
        stmt.setString(++i, ProductName1.getText());
        ..
        stmt.setString(++i, ID2.getText());

        stmt.executeUpdate();
        }


        The above also ensures that the connection is properly closed.








      share|improve this answer


























        0












        0








        0







        At least two issues here:




        1. UPDATE statements do not return the updated row (would be nice if they did...) So you cannot expect to get a ResultSet from an update statement. Had you been using executeUpdate() instead of executeQuery() this would have been easier to spot.



        2. Instead of quoting the parameters when forming the SQL string, it is better to use a PreparedStatement. I.e., use something like:



          String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
          try(Connection conn = SqliteConnection.Connector();
          PreparedStatement stmt = conn.prepareStatement(sql)) {
          int i = 0;
          stmt.setString(++i, ProductName1.getText());
          ..
          stmt.setString(++i, ID2.getText());

          stmt.executeUpdate();
          }


          The above also ensures that the connection is properly closed.








        share|improve this answer













        At least two issues here:




        1. UPDATE statements do not return the updated row (would be nice if they did...) So you cannot expect to get a ResultSet from an update statement. Had you been using executeUpdate() instead of executeQuery() this would have been easier to spot.



        2. Instead of quoting the parameters when forming the SQL string, it is better to use a PreparedStatement. I.e., use something like:



          String sql = "UPDATE " + SupermarketDB + " SET ProductName=?, Barcode=?, Category=?, SubCategory=?, TotalStock=? Where ID=?");
          try(Connection conn = SqliteConnection.Connector();
          PreparedStatement stmt = conn.prepareStatement(sql)) {
          int i = 0;
          stmt.setString(++i, ProductName1.getText());
          ..
          stmt.setString(++i, ID2.getText());

          stmt.executeUpdate();
          }


          The above also ensures that the connection is properly closed.









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 16:05









        nimrodmnimrodm

        17.7k64750




        17.7k64750






























            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%2f53458994%2fsql-not-updating-data%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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno