1

I am having a sql syntax issue when I am trying to update my databse through my update method which is trigured by a button

here is the button code

update.addActionListener(e -> {

               int i = table.getSelectedRow();
               if (i >= 0) {
                   model.setValueAt(PackId.getText(), i, 0);
                   model.setValueAt(PackName.getText(), i, 1);
                   model.setValueAt(VendorName.getText(), i, 2);
                   model.setValueAt(PackValue.getText(), i, 3);
                   try {
                       updatepacks(PackId,PackName,VendorName,PackValue);
                   } catch (SQLException throwables) {
                       throwables.printStackTrace();
                   }

               } else {
                   System.out.println("Update Error");
               }

The update method code

public void updatepacks(JTextField PackId, JTextField PackName, JTextField VendorName, JTextField PackValue) throws SQLException {


        try{
            Connection conn = DriverManager.getConnection("jdbc:sqlite:packsver3.db");
            String sqlupdate = "Update  packs" + " SET PackName = ?" + " VendorName = ?"  + "PackValue = ? " + "Where PackId = ? ";
            try (PreparedStatement ps = conn.prepareStatement(sqlupdate)) {
                ps.setString(1, String.valueOf(PackId));
                ps.setString(2, String.valueOf(PackName));
                ps.setString(3, String.valueOf(VendorName));
                ps.setString(4, String.valueOf(PackValue));
                ps.executeUpdate();
            }


        } catch (Exception e) {
            e.printStackTrace();

            }
        }

And the error

enter image description here

2
  • hmmm, well as a start those ps.setString(1, String.valueOf(PackId)); ps.setString(2, String.valueOf(PackName)); ps.setString(3, String.valueOf(VendorName)); ps.setString(4, String.valueOf(PackValue)); Dont actually match the order of ? you have in your SQL statement..... packName should be first, then vendorName, etc Also I suggest you actually check if they are empty/null :) Commented Apr 5, 2021 at 19:30
  • You could also check the return value of String.valueOf(PackId). For me it is not obvious that this returns the text contained in the JTextField. Commented Apr 5, 2021 at 19:57

1 Answer 1

1

You missed the commas in the UPDATE statement. It should look like:

String sqlupdate = "Update  packs" 
  + " SET PackName = ?, " // added comma at the end
  + " VendorName = ?, " // added comma at the end
  + "PackValue = ? "
  + "Where PackId = ? ";
       
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help I got it to update my gui table now to get the database to show it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.