简单删除查询不能与UCanAccess一起使用

时间:2015-07-15 17:22:58

标签: java sql sql-delete ucanaccess

我使用Netbeans IDE 8.02操作访问数据库,但随着驱动程序的删除,我一直在努力使用UCanAccess。我已经能够将数据插入到数据库中,并且我能够从数据库接收数据,但我很难删除记录,即使这是一个非常简单的查询。

这是我的方法:

  public void VerwyderHemp(String Path, int KledingStukNo){

    try {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

        conn = DriverManager.getConnection("jdbc:ucanaccess://" + Path);
        st = conn.createStatement();

        String sql = "DELETE FROM tblHemde WHERE tblHemde.ID = " + KledingStukNo + ";" ;
        ResultSet rs = st.executeQuery(sql);

        } catch (Exception e) {

            e.printStackTrace();
        }


    }

我在jFrame中的jButton中使用我的方法,它看起来像这样:

private void     jButtonVerwyderHempActionPerformed(java.awt.event.ActionEvent evt)                                                                  {                                                    
    // TODO add your handling code here:
    int KledingstukNo = Integer.parseInt(jTextFieldDeleteHemp.getText());
    myHemp.VerwyderHemp(Path2, KledingstukNo);
    System.out.println(KledingstukNo);
}

参数" Path2"只是指定数据库的位置并没有给出任何问题,因为我在类似的方法中使用它,但我不太确定其他参数" KledingstukNo",尽管我打印出来&#34 ; KledingstukNo",我得到了" jTextFieldDeleteHemp"中输入的值。

当我连接到我的数据库时,我收到以下消息:

警告:表格元数据中的错误tblHemde:表格中元数据中的行数为8,但UCanAccess已找到并加载了9条记录。一切都会正常,但修复数据库会更好。

当我尝试删除记录时,收到以下错误消息:

*

net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException
9
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:202)
    at Klasse.Hemp.VerwyderHemp(Hemp.java:196)
    at GUI.FinalGUI.jButtonVerwyderHempActionPerformed(FinalGUI.java:598)
    at GUI.FinalGUI.access$600(FinalGUI.java:27)
    at GUI.FinalGUI$7.actionPerformed(FinalGUI.java:328)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:199)
    ... 40 more
Caused by: org.hsqldb.HsqlException: java.lang.NullPointerException
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.result.Result.newErrorResult(Unknown Source)
    at org.hsqldb.StatementDMQL.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 43 more
Caused by: java.lang.NullPointerException
    at net.ucanaccess.jdbc.UcanaccessConnection.getCtxConnection(UcanaccessConnection.java:87)
    at net.ucanaccess.triggers.TriggerDelete.fire(TriggerDelete.java:33)
    at org.hsqldb.TriggerDef.pushPair(Unknown Source)
    at org.hsqldb.Table.fireTriggers(Unknown Source)
    at org.hsqldb.StatementDML.delete(Unknown Source)
    at org.hsqldb.StatementDML.executeDeleteStatement(Unknown Source)
    at org.hsqldb.StatementDML.getResult(Unknown Source)
    ... 47 more
BUILD SUCCESSFUL (total time: 24 seconds)*

我没有看到出现此错误的原因,请帮忙!

2 个答案:

答案 0 :(得分:1)

这是语句界面的不正当和意外使用。 不要使用

st.executeQuery(sql);

使用delete sql语句 但是st.execute(sql);st.executeUpdate(sql);

答案 1 :(得分:1)

您没有进行选择查询,因此无法使用st.executeQuery(sql);您必须使用st.executeUpdate();进行删除,插入或更新查询。