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

时间:2012-12-10 18:12:26

标签: java database-connection sql-update resultset

我得到了无法正常工作的Update 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;
}

这是我的HentbestillingsordrereHandler

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)

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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语句。