views:

85

answers:

2

Hi

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

A: 

Check out this similar question.

Adamski
thks but its not for sqlite database, my code works well with mysql or oracle the error says " sqlite only support type forward only but when i set resultset on type forward only, i have an error says " resultset is type forward only" i donno what to do to make worke on sqlite cause i have to use sqlite for my application (sqlite don't need a dedicated server, juste needs a db file)
tuxou
The fact that you're using SQLLite isn't the problem - It's the call to ResultSet beforeFirst() which isn't supported by TYPE_FORWARD ResultSets. You shouldn't need to make this call though; You should be using an AbstractTableModel based on a dynamic data structure such as a List, rather than a fixed-size Object[][] array.
Adamski
but how ? ... :(
tuxou
How what? The question I've linked to includes my answer which has example code on how to do this.
Adamski
+1  A: 

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

mdma