java jdbc ms 访问 "Syntax error in UPDATE statement"

标签 java sql jdbc odbc ms-access-2010

我正在尝试更新我的数据库,但我的代码中出现了一些语法错误。请帮助。我尝试将其更改为“st.execute(statement);”和“st.executeUpdate(声明);”但错误仍然存​​在。 这是我的代码...

public void editBook(String inputTitle, String[] newBookInfo)
{
    boolean result = false;
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:database");
        st = con.createStatement();
        rs = st.executeQuery("select * from library");

        while(rs.next())
        {
            if(boyerMoore(rs.getString("Title").toLowerCase(), inputTitle.toLowerCase()))
            {
                isbn = rs.getString("ISBN");
                statement = String.format("UPDATE library SET ISBN = '%s', Title = '%s', Author = '%s', Publisher = '%s', Published Year = '%s', Available Copies = '%s', Total Copies = '%s', WHERE ISBN = '%s'", newBookInfo[0], newBookInfo[1], newBookInfo[2], newBookInfo[3], newBookInfo[4], newBookInfo[5], newBookInfo[6], isbn);
                st.executeQuery(statement);
                rs.close();
                st.close();
                con.close();

                JOptionPane.showMessageDialog(null, "Edit Succes", "Succes", JOptionPane.PLAIN_MESSAGE);
                result = true;
            }
        }

        if(!result)
            JOptionPane.showMessageDialog(null, "\"" + inputTitle + "\"  not Found in the Library", "Error", JOptionPane.ERROR_MESSAGE);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

这是完整的堆栈跟踪..

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at dataBase.editBook(dataBase.java:161)
at mainFrame.actionPerformed(mainFrame.java:177)
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$200(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)

最佳答案

将包含空格的字段名称括起来:[Published Year][可用副本][总份数]。无论您是继续像以前一样构建 UPDATE 语句还是切换到参数查询,您仍然必须将这些名称括起来。

如前所述,丢弃 WHERE 之前的逗号 ...

[Total Copies] = '%s', WHERE ISBN
                     ^

如果在进行这些更改后出现“类型不匹配”错误,请检查目标字段的数据类型。目前您的声明正在为这些字段提供文本值。如果有数字而不是文本,请消除更新值周围的单引号。

关于java jdbc ms 访问 "Syntax error in UPDATE statement",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19064271/

相关文章:

java - JDBCTemplate + MySQL 主键转外键

java - 模拟长时间运行的 Oracle 数据库查询

java - EasyMock 链式方法调用产生空指针异常

java - 在R代码中调用JAVA代码的参数动态传递csv文件和唯一ID

SQL 循环从不同的开始和结束日期生成许多行

mysql - 在 SQL 语句中使用 % 的性能变化

php - 当我将 boolean 值 True-False 作为绑定(bind)到 int 字段的参数发送到 PDO 语句时会发生什么?

java - 使用 JPA : what queries should I write to get the same result I get in JDBC? 加入

java - 如何在 RecyclerView.Adapter 中使用 onActivityResult

java - Mooc Helsinki 第 1 部分第 4 周练习 18 Java