user3444341
user3444341

Reputation: 31

Error when calling getColumnCount() from ResultSet metadata

I seem to be have a problem when trying to get the coulmn count from a resultset's metadata. THe error is Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state.

THe idea is to use this table model to populate a JTable with the result from the database query. However when I do I get the above error.

The relevant code is:

public DefaultTableModel buildFlightModel()
            throws SQLException {

         query="SELECT Airline.AirlineName,  Flight.FlightID, Flight.Location, Flight.Destination, Flight.ArriveTime, Flight.LeaveTime FROM Flight INNER JOIN Airline ON Airline.AirlineID=Flight.AirlineID;";

            try {
                Class.forName("org.sqlite.JDBC");
                c = DriverManager.getConnection("jdbc:sqlite:coursework.db");
                stmt = c.createStatement();
                rs=stmt.executeQuery(query);
                while(rs.next()){
                    //System.out.println(s);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        ResultSetMetaData metaData = rs.getMetaData();

        // 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<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);

    }

Any thoughts/ideas/help would be deeply appreciated. Thank you.

Upvotes: 2

Views: 863

Answers (1)

J&#252;rgen Schmidt
J&#252;rgen Schmidt

Reputation: 11

This exception is thrown when ResultSet.isclose() == true. Try to create a Clone of the MetaData before starting the Read-Loop.

Upvotes: 1

Related Questions