无法通过java在sql数据库中写入数据但是读取成功

时间:2017-08-04 06:05:57

标签: java jdbc java-8 jdbc-odbc mssql-jdbc

我使用以下代码:

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

我正在从SQL数据库中检索数据,但每当我尝试插入数据时,我都会在运行时遇到错误:

  

java.sql.SQLException:[Microsoft] [ODBC SQL Server驱动程序] [SQL   服务器]关键字' check'附近的语法不正确。在   sun.jdbc.odbc.JdbcOdbc.createSQLException(未知来源)at at   sun.jdbc.odbc.JdbcOdbc.standardError(未知来源)at   sun.jdbc.odbc.JdbcOdbc.SQLExecute(未知来源)at   sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(未知来源)at at   AdminPanel $ 2.actionPerformed(AdminPanel.java:184)at   javax.swing.AbstractButton.fireActionPerformed(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 300(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

'检查'是一个保留的关键字,因此代码失败,尝试使用不是关键字的名称可以解决问题。