开发者

display sqlite datatable in a jtable

开发者 https://www.devze.com 2022-12-31 13:21 出处:网络
I\'m trying to display an sqlite data table in a jtable but i have an error \" sqlite is type forward only\"

I'm trying to display an sqlite data table in a jtable but i have an error " sqlite is type forward only"

how could I display it in a jtable

               try {
        long start = System.currentTimeMillis();

                    Statement state = ConnectionBd.getInstance().createStatement(
                   ResultSet.TYPE_SCROLL_INSENSITIVE, 
                  开发者_开发技巧                                  ResultSet.CONCUR_READ_ONLY

   );
    ResultSet res = state.executeQuery("SELECT * FROM data");

        ResultSetMetaData meta = res.getMetaData();

        Object[] column = new Object[meta.getColumnCount()];

        for(int i = 1 ; i <= meta.getColumnCount(); i++){
            column[i-1] = meta.getColumnName(i);
        }

        res.last();
        int rowCount = res.getRow();
        Object[][] data = new Object[res.getRow()][meta.getColumnCount()];

        res.beforeFirst();
        int j = 1;

        while(res.next()){
            for(int i = 1 ; i <= meta.getColumnCount(); i++)
                data[j-1][i-1] = res.getObject(i);

            j++;
        }

        res.close();
        state.close();

        long totalTime = System.currentTimeMillis() - start;
        result.removeAll();
        result.add(new JScrollPane(new JTable(data, column)), BorderLayout.CENTER);
        result.add(new JLabel("execute in " + totalTime + " ms and has " + rowCount + " ligne(s)"), BorderLayout.SOUTH);
        result.revalidate();

    } catch (SQLException e) {
        result.removeAll();
        result.add(new JScrollPane(new JTable()), BorderLayout.CENTER);
        result.revalidate();
        JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR ! ",                      JOptionPane.ERROR_MESSAGE);
    }

thank you


The call to res.last() is what is causing the trouble. If you want to know how many rows there are, then you can either issue first a SELECT count(*) FROM (<your-query> ) base or simpler, use an ArrayList rather than an object array to hold the rows. (You can still use Object[] for each row, since the number of columns is known ahead of time.)

0

精彩评论

暂无评论...
验证码 换一张
取 消