gpt4 book ai didi

java - 简单删除查询不适用于 UCanAccess

转载 作者:行者123 更新时间:2023-12-01 22:13:51 24 4
gpt4 key购买 nike

我正在使用 Netbeans IDE 8.02 来操作 Access 数据库,但随着驱动程序的删除,我一直在与 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)*

我没有找到此错误的原因,请帮忙!

最佳答案

这是对语句接口(interface)的不当且意外的使用。不要使用

st.executeQuery(sql);

带有删除sql语句但是st.execute(sql);st.executeUpdate(sql);

关于java - 简单删除查询不适用于 UCanAccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com