I'm trying to get data from an Access table and show it on a JTable. I'm using UCanAccess as Java 8 does not support JDBC-ODBC.
My window class calls the charging methods:
ctrlGestionVentas= new CtrlGestionVentas();
ctrlGestionVentas.cargarListaVentas(tblListaVentas);
Then, CtrlGestionVentas.cargarListaVentas(tabla) fills the JTable from the ResultSet:
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);
}
}
Here's the ResultSet getter method, in DataVentas class (note: FactoryConexion has the ConnectionString, ommited in this 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;
}
}
When I execute, I get the following:
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
Note that the last line marked in the debugger is the one at buildTableModel method (rs.getMetaData()). What's going on here?
Thanks for your time!
That the method that gets the ResultSet, then closes it in the finally block. So you can't use that ResultSet at all. You have to re-organize your code.