-1

我得到了不起作用的更新 sql,我似乎无法使其工作。我不知道哪里出了问题,我得到了 Nullpointer 异常。

这是我的 KaldSQL 课程

public ResultSet opdatereOrdre(Connection con, int BestillingsID, int Modtager){

    ResultSet opdatereOrdre = null;

    try {
        PreparedStatement stmt = con.prepareStatement("UPDATE varebestillinger set BestillingsStatus=1, ModtagetAf ="+Modtager+" where BestillingsID="+BestillingsID);
        opdatereOrdre = stmt.executeQuery();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return opdatereOrdre;
}

这是我的 HentbestillingsordreHandler

public void actionPerformed(ActionEvent e) {
                        System.out.println(hentordregistrer.BestillingsID);
                        try {

                            con = ks.connectNow();
                            ResultSet rs = ks.opdatereOrdre(con, hentordregistrer.BestillingsID, 1);

                        } catch (ClassNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }


                    }
                });

我得到的错误是

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:412)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1794)
    at KaldSQL.opdatereOrdre(KaldSQL.java:126)
    at HentbestillingsordreHandler$1.actionPerformed(HentbestillingsordreHandler.java:120)
    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$000(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

2 回答 2

2

这看起来不像 NPE,看起来您需要使用 executeUpdate 而不是 executeQuery。你可以试试这个:

...
con.createStatement();
opdatereOrdre = stmt.executeUpdate("UPDATE varebestillinger set BestillingsStatus=1, ModtagetAf ="+Modtager+" where    BestillingsID="+BestillingsID);
...
于 2012-12-10T18:18:03.600 回答
0

使用 JDBC 执行查询主要有三种有用的方法,下面是对所有这些方法的解释,并了解在哪里使用这些方法

1. boolean execute()

执行此 PreparedStatement 对象中的 SQL 语句,该对象可以是任何类型的 SQL 语句。

2. ResultSet executeQuery()

在此 PreparedStatement 对象中执行 SQL 查询,并返回查询生成的 ResultSet 对象。

3 . int executeUpdate()

执行此 PreparedStatement 对象中的 SQL 语句,该语句必须是 SQL INSERT、UPDATE 或 DELETE 语句;或不返回任何内容的 SQL 语句,例如 DDL 语句。

于 2012-12-10T18:23:42.437 回答