我正在使用uCanAccess库通过swing GUI将行插入现有的MS Access表中。代码工作正常,直到 rs.insertRow(),之后光标不能设置为任何地方。
这是我的代码:
private void addRowBtnActionPerformed(java.awt.event.ActionEvent evt) {
try {
rs.moveToInsertRow();
System.out.println("moved to insert Row");
rs.updateString(2, "jimmyHendrix");
System.out.println("updated string");
rs.updateInt(3, 5);
System.out.println("updated int");
rs.insertRow();
System.out.println("Row Inserted"); // works great till here
rs.moveToCurrentRow(); // rs.getRow() returns a 0
System.out.println("cursor moved to current row"); // This line does get printed
rs.absolute(1); // Throws the above mentioned exception at any attempt to set the cursor position
System.out.println("abs 1"); // Hence this line doesn't get printed.
} catch (SQLException ex) {
Logger.getLogger(accessGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是声明的创建和执行:
public class accessGUI extends javax.swing.JFrame {
Connection conn;
Statement s;
ResultSet rs;
/**
* Creates new form accessGUI
*/
public accessGUI() {
initComponents();
try {
conn = DriverManager.getConnection("jdbc:ucanaccess://d:/accessFiles/records.accdb;memory=true");
s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = s.executeQuery("SELECT [RollNo],[Name],[class] FROM [schoolRecord]");
} catch (SQLException ex) {
Logger.getLogger(accessGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
以下是生成的输出,它显示了异常堆栈跟踪:
>--- exec-maven-plugin:1.2.1:exec (default-cli) @ accessMaven ---
moved to insert Row
updated string
updated int
Row Inserted
cursor moved to current row
Aug 27, 2017 4:33:06 PM com.maxeejava.accessmaven.accessGUI addRowBtnActionPerformed
SEVERE: null
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.2 invalid cursor state: cannot FETCH NEXT, PRIOR, CURRENT, or RELATIVE, cursor position is unknown
at net.ucanaccess.jdbc.UcanaccessResultSet.absolute(UcanaccessResultSet.java:92)
at com.maxeejava.accessmaven.accessGUI.addRowBtnActionPerformed(accessGUI.java:243)
at com.maxeejava.accessmaven.accessGUI.access$500(accessGUI.java:19)
at com.maxeejava.accessmaven.accessGUI$6.actionPerformed(accessGUI.java:116)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.sql.SQLException: invalid cursor state: cannot FETCH NEXT, PRIOR, CURRENT, or RELATIVE, cursor position is unknown
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlExceptionSQL(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.absolute(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessResultSet.absolute(UcanaccessResultSet.java:90)
... 39 more
Caused by: org.hsqldb.HsqlException: invalid cursor state: cannot FETCH NEXT, PRIOR, CURRENT, or RELATIVE, cursor position is unknown
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 42 more
检查数据库后,新行会添加,但在插入新行后,在运行时尝试设置光标位置失败。