1

我正在尝试更新我的数据库,但我的代码中出现了一些语法错误..请帮助..我尝试将其更改为“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)
4

5 回答 5

1

在 where 之前删除 ','

, WHERE 

更新库 SET ISBN =“%s”,标题 =“%s”,作者 =“%s”,出版商 =“%s”,出版年份 =“%s”,可用副本 =“%s”,总副本 = '%s' WHERE ISBN = '%s'", newBookInfo[0], newBookInfo[1], newBookInfo[2], newBookInfo[3], newBookInfo[4], newBookInfo[5], newBookInfo[6], isbn

于 2013-09-28T06:57:53.413 回答
1

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

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

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

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

于 2013-09-28T07:16:52.340 回答
1

有几件事混淆了。

        Connection con = DriverManager.getConnection("jdbc:odbc:database");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SELECT Title, ISBN FROM library");
        PreparedStatement statement = con
                .prepareStatement("UPDATE library "
                        + "SET ISBN = ?, "
                        + "Title = ?, "
                        + "Author = ?, "
                        + "Publisher = ?, "
                        + "[Published Year] = ?, "
                        + "[Available Copies] = ?, " // [...] needed.
                        + "[Total Copies] = ? " // No comma.
                        + "WHERE ISBN = ?");

        while (rs.next()) {
            if (boyerMoore(rs.getString("Title").toLowerCase(),
                    inputTitle.toLowerCase())) {
                isbn = rs.getString("ISBN");
                statement.setString(1, newBookInfo[0]);
                statement.setString(2, newBookInfo[1]);
                statement.setString(3, newBookInfo[2]);
                statement.setString(4, newBookInfo[3]);
                statement.setString(5, newBookInfo[4]);
                statement.setString(6, newBookInfo[5]);
                statement.setString(7, newBookInfo[6]);
                // statement.setInt(7, Integer.parseInt(newBookInfo[6]));
                statement.setString(8, isbn);
                statement.executeUpdate();

                JOptionPane.showMessageDialog(null, "Edit Succes",
                        "Succes", JOptionPane.PLAIN_MESSAGE);
                result = true;
            }
        }
        rs.close(); // Here.
        st.close();
        statement.close();
        con.close();
于 2013-09-28T07:18:38.317 回答
0

删除where子句前的逗号。也尝试使用PreparedStatement而不是Statement 使用这种方式用于 PreparedStatement

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

rs=pt.executeQuery();
        while(rs.next())
        {

//your codes
        }


    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
于 2013-09-28T07:02:42.267 回答
0

使用两个不同的 Statement 对象,一个用于 Read 另一个用于更新。或者关闭现有语句并重新实例化。

于 2013-09-28T14:42:42.383 回答