Display sqlite datatable in jtable

I am trying to display sqlite data table in jtable, but I have the error "sqlite - only type forward"

how can i display it in 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

+3
source share
1 answer

The challenge res.last()is causing problems. If you want to know how many lines there are, then you can first issue a SELECT count(*) FROM (<your-query> ) baseor simpler, use an ArrayList rather than an array of objects to hold the lines. (You can use Object [] for each row, as the number of columns is known in advance.)

+1
source

Source: https://habr.com/ru/post/1745780/


All Articles