0

我正在使用 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”,尽管当我打印出“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)*

我没有看到这个错误的原因,请帮助!

4

2 回答 2

2

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

于 2015-07-15T21:22:18.863 回答
1

这是对语句接口的不当和意外使用。不要使用

st.executeQuery(sql);

使用删除 sql 语句但st.execute(sql);st.executeUpdate(sql);

于 2015-07-15T18:02:29.427 回答