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 

UPDATE ライブラリ 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 にこの方法を使用するPreparedStatement代わりに使用してみてくださいStatement

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 に答える