UCanAccess 异常:无法从结果集中获取元数据(无效游标状态)
UCanAccess Exception: cannot getMetaData from ResultSet (invalid cursor state)
我正在尝试从 Access table 获取数据并将其显示在 JTable 上。我正在使用 UCanAccess,因为 Java 8 不支持 JDBC-ODBC。
我的windowclass调用的收费方式:
ctrlGestionVentas= new CtrlGestionVentas();
ctrlGestionVentas.cargarListaVentas(tblListaVentas);
然后,CtrlGestionVentas.cargarListaVentas(tabla) 从结果集中填充 JTable:
public class CtrlGestionVentas {
public void cargarListaVentas(JTable tabla) {
ResultSet rs;
DataVentas dv = new DataVentas();
rs = dv.getListaVentas();
try {
tabla.setModel(buildTableModel(rs));
RowSorter sorter = new TableRowSorter(buildTableModel(rs));
tabla.setRowSorter(sorter);
tabla.getTableHeader().setDefaultRenderer(new MultiSortTableCellHeaderRenderer());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData(); /*THIS IS THE LINE WHICH APPARENTLY PROVOKES THE ERROR*/
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
这是 DataVentas class 中的 ResultSet getter 方法(注意:FactoryConexion 具有 ConnectionString,在此 post 中省略):
public class DataVentas {
public ResultSet getListaVentas() {
PreparedStatement stmt=null;
ResultSet rs=null;
try {
stmt = FactoryConexion.getInstancia().getConn().prepareStatement("select * from ventasfinal");
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
finally{
try {
if (rs!=null)
rs.close();
if (stmt!=null)stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
}
当我执行时,我得到以下信息:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 estado del cursor incorrecto: cursor indicado no está abierto
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:480)
at negocio.CtrlGestionVentas.buildTableModel(CtrlGestionVentas.java:37)
at negocio.CtrlGestionVentas.cargarListaVentas(CtrlGestionVentas.java:22)
at ui.GestionVentas.<init>(GestionVentas.java:85)
at ui.GestionVentas.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
Caused by: java.sql.SQLException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getMetaData(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:478)
... 18 more
Caused by: org.hsqldb.HsqlException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 22 more
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 estado del cursor incorrecto: cursor indicado no está abierto
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:480)
at negocio.CtrlVendedores.buildTableModel(CtrlVendedores.java:38)
at negocio.CtrlVendedores.cargarListaVendedores(CtrlVendedores.java:23)
at ui.GestionVentas.<init>(GestionVentas.java:95)
at ui.GestionVentas.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
Caused by: java.sql.SQLException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getMetaData(Unknown Source)
at
net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:478)
... 18 more
Caused by: org.hsqldb.HsqlException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 22 more
请注意,调试器中标记的最后一行是 buildTableModel 方法 (rs.getMetaData()) 中的那一行。这是怎么回事?
感谢您的宝贵时间!
获取结果集的方法,然后在 finally 块中将其关闭。所以你根本不能使用那个 ResultSet。你必须重新组织你的代码。
我正在尝试从 Access table 获取数据并将其显示在 JTable 上。我正在使用 UCanAccess,因为 Java 8 不支持 JDBC-ODBC。
我的windowclass调用的收费方式:
ctrlGestionVentas= new CtrlGestionVentas();
ctrlGestionVentas.cargarListaVentas(tblListaVentas);
然后,CtrlGestionVentas.cargarListaVentas(tabla) 从结果集中填充 JTable:
public class CtrlGestionVentas {
public void cargarListaVentas(JTable tabla) {
ResultSet rs;
DataVentas dv = new DataVentas();
rs = dv.getListaVentas();
try {
tabla.setModel(buildTableModel(rs));
RowSorter sorter = new TableRowSorter(buildTableModel(rs));
tabla.setRowSorter(sorter);
tabla.getTableHeader().setDefaultRenderer(new MultiSortTableCellHeaderRenderer());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData(); /*THIS IS THE LINE WHICH APPARENTLY PROVOKES THE ERROR*/
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
这是 DataVentas class 中的 ResultSet getter 方法(注意:FactoryConexion 具有 ConnectionString,在此 post 中省略):
public class DataVentas {
public ResultSet getListaVentas() {
PreparedStatement stmt=null;
ResultSet rs=null;
try {
stmt = FactoryConexion.getInstancia().getConn().prepareStatement("select * from ventasfinal");
rs = stmt.executeQuery();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
finally{
try {
if (rs!=null)
rs.close();
if (stmt!=null)stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
}
当我执行时,我得到以下信息:
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 estado del cursor incorrecto: cursor indicado no está abierto
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:480)
at negocio.CtrlGestionVentas.buildTableModel(CtrlGestionVentas.java:37)
at negocio.CtrlGestionVentas.cargarListaVentas(CtrlGestionVentas.java:22)
at ui.GestionVentas.<init>(GestionVentas.java:85)
at ui.GestionVentas.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
Caused by: java.sql.SQLException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getMetaData(Unknown Source)
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:478)
... 18 more
Caused by: org.hsqldb.HsqlException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 22 more
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 estado del cursor incorrecto: cursor indicado no está abierto
at net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:480)
at negocio.CtrlVendedores.buildTableModel(CtrlVendedores.java:38)
at negocio.CtrlVendedores.cargarListaVendedores(CtrlVendedores.java:23)
at ui.GestionVentas.<init>(GestionVentas.java:95)
at ui.GestionVentas.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
Caused by: java.sql.SQLException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getMetaData(Unknown Source)
at
net.ucanaccess.jdbc.UcanaccessResultSet.getMetaData(UcanaccessResultSet.java:478)
... 18 more
Caused by: org.hsqldb.HsqlException: estado del cursor incorrecto: cursor indicado no está abierto
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 22 more
请注意,调试器中标记的最后一行是 buildTableModel 方法 (rs.getMetaData()) 中的那一行。这是怎么回事?
感谢您的宝贵时间!
获取结果集的方法,然后在 finally 块中将其关闭。所以你根本不能使用那个 ResultSet。你必须重新组织你的代码。