Java 为什么我的更新结果集不起作用

Java 为什么我的更新结果集不起作用,java,database-connection,sql-update,resultset,Java,Database Connection,Sql Update,Resultset,我得到了不起作用的更新sql,我似乎无法让它起作用。我不知道哪里出了问题,我得到了一个例外 这是我的KaldSQL类 public ResultSet opdatereOrdre(Connection con, int BestillingsID, int Modtager){ ResultSet opdatereOrdre = null; try { PreparedStatement stmt = con.prepareStatement("UPDATE

我得到了不起作用的更新sql,我似乎无法让它起作用。我不知道哪里出了问题,我得到了一个例外

这是我的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)

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

...
con.createStatement();
opdatereOrdre = stmt.executeUpdate("UPDATE varebestillinger set BestillingsStatus=1, ModtagetAf ="+Modtager+" where    BestillingsID="+BestillingsID);
...

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

1. boolean execute()
执行此PreparedStatement对象中的SQL语句,该对象可以是任何类型的SQL语句

2. ResultSet executeQuery()
在此PreparedStatement对象中执行SQL查询,并返回查询生成的ResultSet对象

3 . int executeUpdate()

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

注意从方法返回后如何使用
opdatereOrdre
。如果由于任何原因失败,则返回空引用。如果您稍后尝试使用它,您将得到一个NPE。要执行更新,请不要使用
executeQuery
,使用,并且正如Stacktrace所示,您没有NPE,您有一个
SQLException
,您正面临着完全相同的问题。您应该建议用户使用绑定参数,永远不要将它们与SQL关联……也许。如果参数化类型更复杂(比如日期),并且希望JDBC管理类型转换,我同意您的看法。但是,当使用带有索引的原始参数化JDBC时,我相信您可能会使索引出错,从而使代码更脆弱。即使您不在代码上进行测试(这是您应该做的),第一次尝试时,错误的参数索引将立即失败,并且每次尝试时都会失败。使用串联,代码似乎可以工作,但是使用
--
或类似的方法,它可能会突然失败,导致严重的后果。这就是我认为更脆弱的原因。在编译时或运行时,细微的错误不会失败:setInt(1,BestillingsID)和setInt(2,Modtager)。
2. ResultSet executeQuery()