How to get the values from individual columns in an SQL Database with Java: UcanaccessSQLException

22 views Asked by At

I have a table in a database which I have displayed in a JTable on a JFrame. I made the connection to the database and everything displays perfectly when I run the program. When I try to get the values of individual fields using getInt and getString methods it doesn't work and I run into errors.

In my main method I call

msConnectDB(tblDatabase, "SELECT * FROM tblGeneral");

The msConnectDB method is as follows:

public void msConnectDB(JTable tbl, String query){
        try{
            if(query != null){
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

                // a connection is established without any errors and the query is called successfully
                connection = DriverManager.getConnection("jdbc:ucanaccess://C://Users//...//MyDB.accdb");
                Statement st = connection.createStatement();
                resultSet = st.executeQuery(query);
         
                // the resultSet is displayed in the table
                tbl.setModel(DbUtils.resultSetToTableModel(resultSet));
                
                // this does not return anything
                System.out.println(DbUtils.resultSetToNestedList(resultSet));
                
                // this does not print anything
                while(resultSet.next()) {
                    System.out.print(resultSet.getInt("ID"));
                    System.out.print(resultSet.getDate("DateReported"));
                    System.out.print(resultSet.getDouble("Price"));
                    System.out.print(resultSet.getInt("Style"));
                    System.out.print(resultSet.getInt("OriginID"));
                    System.out.print(resultSet.getInt("DestinationID"));
                    
                }
            }
            else{
                tbl.removeAll();
            }
            
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }

When I call System.out.println(DbUtils.resultSetToNestedList(resultSet)) it returns an empty list []

Nothing else is printed other than []

The table is drawn in tbl.setModel(DbUtils.resultSetToTableModel(resultSet)); and looks like this:

enter image description here

Before the JFrame is displayed I get the following error in a message dialog:

netucanaccess.jdbc.UcanaccessSQLException: UCAExc:::5.0.1 invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is positioned after last row

Any help would be appreciated!

0

There are 0 answers