0

I'm using the code below:

JButton btnEdit = new JButton("Edit");
btnEdit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        try{
        String query = "Insert Into check(Name, Password)Values(?,?)";
        PreparedStatement pst = con.prepareStatement(query);
        pst.setString(1, textField.getText());
        pst.setString(2, textField_1.getText());
/*      pst.setString(3, textField_2.getText());
        pst.setString(4, textField_4.getText());
        pst.setString(5, textField_5.getText());
        */

        pst.execute();
        JOptionPane.showMessageDialog(null, "Data Saved");
        pst.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
});

I am retrieving data from a SQL database, but whenever I try to insert data, I get errors during runtime:

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'check'. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source) at AdminPanel$2.actionPerformed(AdminPanel.java:184) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$300(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run (Unknown Source)

7
  • 1
    try adding a space before values Commented Aug 4, 2017 at 6:06
  • Try adding a semicolon at the end of the sql statement. String query = "Insert Into check(Name, Password)Values(?,?);"; Commented Aug 4, 2017 at 6:16
  • Still getting same error log. tried both of your suggestions bro Commented Aug 4, 2017 at 6:19
  • 2
    Can 'check' be a word that can't be used in the sqls, may be a reserved word? Instead of 'check' try 'abc' and see if you get same error... Commented Aug 4, 2017 at 6:22
  • Seems like deepakl is right. Its a sql keyword to put constraints. Commented Aug 4, 2017 at 6:24

1 Answer 1

1

'check' is a reserved keyword and so code is failing, trying with a name which is not a keyword should solve the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Using INSERT INTO [check] ( ... would also fix the problem without having to change the table name.

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.