在 jtable 中显示 sqlite 数据表

发布于 2024-09-01 15:34:22 字数 1726 浏览 0 评论 0原文

我正在尝试在 jtable 中显示 sqlite 数据表,但出现错误“sqlite 仅向前键入”

如何在 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);
    }

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-09-08 15:34:22

res.last() 的调用是造成问题的原因。如果您想知道有多少行,那么您可以首先发出 SELECT count(*) FROM () base 或更简单,使用 ArrayList 而不是对象数组来保存行。 (您仍然可以对每行使用 Object[],因为列数是提前知道的。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文