我正在尝试从Access表中获取数据并在JTable上显示它。我正在使用UCanAccess,因为Java 8不支持JDBC-ODBC。
我的窗口类调用充电方法:
ctrlGestionVentas= new CtrlGestionVentas();
ctrlGestionVentas.cargarListaVentas(tblListaVentas);
然后,CtrlGestionVentas.cargarListaVentas(tabla)从ResultSet填充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类中的ResultSet getter方法(注意:FactoryConexion具有ConnectionString,在本文中已经省略):
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$1.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.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$1.run(GestionVentas.java:63)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.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())中的一行。这是怎么回事?
谢谢你的时间!
答案 0 :(得分:1)
获取ResultSet的方法,然后在finally块中关闭它。因此,您根本无法使用该ResultSet。您必须重新组织代码。