从ResultSet元数据调用getColumnCount()时出错

时间:2017-03-31 19:42:48

标签: java sqlite jtable resultset defaulttablemodel

在尝试从结果集的元数据中获取库存计数时,我似乎遇到了问题。错误是线程中的异常" AWT-EventQueue-0" java.lang.IllegalStateException:SQLite JDBC:内部状态不一致。

这个想法是使用这个表模型用数据库查询的结果填充JTable。但是,当我这样做时,我得到上述错误。

相关代码是:

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

    }

任何想法/想法/帮助都将深表感谢。谢谢。

1 个答案:

答案 0 :(得分:1)

ResultSet.isclose() == true时抛出此异常。尝试在开始读取循环之前创建Clone MetaData