使用按钮单击 - java swing查询替换jtable中的先前行

时间:2017-07-10 05:57:27

标签: java mysql swing pagination jtable

我是jtable的新手,我正在使用h2数据库进行处理。我的表中有数百万条记录,因此我使用分页从DB获取每页所需的数据。到目前为止,我已经完成了创建分页按钮,并根据页码从查询中获取数据。

例如,限制设置为100000,如果我在page 1,则查询中的偏移量为0,如果我在page 2,则偏移将为200001等等......

我正在做什么,我正在拨打eventActionListener或分页按钮。这个数字写在按钮上。将其与recorsPerPageadd 1相乘。这给了我正确的偏移量,然后我将新的偏移量传递给查询并获得新的结果。

我需要的是获得新行后的内容。应删除或隐藏上一行,并在jtable上嵌入新行。我跳了一下我解释了这个问题。

所以,这是我的代码:

public class Try {

    static Connection conn;
    static JPanel panel;
    static JTable table;
    static JButton buttons;
    static int buttonID;
    static int totalRows;
    static int recordPerPage = 100000;
    static int totalPages = 0;
    static int offSet = 0;

    private static JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowGUI();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        });
    }

    protected static void createAndShowGUI() throws SQLException {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setBounds(30, 50, 1300, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        frame.setContentPane(contentPane);

        UIManager.put("TabbedPane.selected", Color.lightGray);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        frame.add(tabbedPane);

        panel = new JPanel();
        tabbedPane.addTab("TABLE", null, panel, null);
        tabbedPane.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 16));
        panel.setBackground(Color.white);

        JPanel panel_1 = new JPanel();
        tabbedPane.addTab("GRAPH", null, panel_1, null);
        panel_1.setBackground(Color.white);

        JTable table = new JTable();
        panel.add(table);
        table.setFillsViewportHeight(true);

        createDBConnection();
    }

    private static void createDBConnection() throws SQLException {
        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");
        } catch (SQLException e) {
            System.out.println("Unable to make connection with DB");
            e.printStackTrace();
        }
        createPaginationButtons(conn);
    }

    private static void createPaginationButtons(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT count(*) FROM cdr");
        while (rs.next()) {
            totalRows = rs.getInt(1);
        }
        int v = totalRows % recordPerPage == 0 ? 0 : 1;
        totalPages = totalRows / recordPerPage + v;
        createButton(totalPages);
    }

    private static void createButton(int totalPages) throws SQLException {
        for (int i = 0; i < totalPages;) {
            buttons = new JButton("" + (i + 1));
            buttons.addActionListener(listener);
            panel.add(buttons);
            i++;
        }
        createTable(offSet);

    }

    static ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                String text = ((JButton) e.getSource()).getText();
                buttonID = Integer.parseInt(text);
                offSet = (buttonID * recordPerPage) + 1;
                try {
                    createTable(offSet);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    };

    private static void createTable(int offSet) throws SQLException {
        table = new JTable();
        Statement stmt1 = conn.createStatement();
        JOptionPane.showMessageDialog(null, recordPerPage);
        JOptionPane.showMessageDialog(null, offSet);

        ResultSet rs1 = stmt1.executeQuery(
                "SELECT ANUMBER,BNUMBER,DATETIME FROM CDR LIMIT '" + recordPerPage + "' OFFSET '" + offSet + "' ");
        JOptionPane.showMessageDialog(null, offSet);
        table.setModel(DbUtils.resultSetToTableModel(rs1));
        panel.add(new JScrollPane(table));

    }



}

offset方法中createTable()的值根据我的需要而改变,但它不会改变表格行?如何嵌入新行并隐藏/删除之前的?

2 个答案:

答案 0 :(得分:1)

基本想法是你只需要更新TableModel

目前,您使用DbUtils每次调用时都会创建一个新的TableModel,而您可以这样做,但效率不高,会影响JTable的其他属性(如列宽)

更好的解决方案是使用DefaultTableModel并自行更新,例如......

private DefaultTableModel model;

private void loadTable(int offset) throws SQLException {
    try (PreparedStatement stmt = conn.prepareStatement("SELECT ANUMBER,BNUMBER,DATETIME FROM CDR LIMIT ? OFFSET ?")) {
        stmt.setInt(1, recordPerPage);
        stmt.setInt(1, offset);
        try (ResultSet rs = stmt.executeQuery()) {
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if (model == null) {
                String[] columnNames = new String[columnCount];
                for (int col = 0; col < columnCount; col++) {
                    columnNames[col] = rsmd.getColumnName(col + 1);
                }
                model = new DefaultTableModel(columnNames, 0);
                table.setModel(model);
            }
            model.setRowCount(0);
            while (rs.next()) {
                Object[] row = new Object[columnCount];
                for (int col = 0; col < columnCount; col++) {
                    row[col] = rs.getObject(col + 1);                        
                }
                model.addRow(row);
            }
        }
    }
}

这假设您之前已创建JTable并将其添加到GUI

答案 1 :(得分:0)

所以,这是我在原始代码中更改的内容,并且可以正常工作

private static void createTable(int offSet) throws SQLException {

        model.addColumn("ANUMBER");
        model.addColumn("BNUMBER");
        model.addColumn("DATETIME");

        panel.add(new JScrollPane(table));
        populateTable(offSet);
    }

    private static void populateTable(int offSet2) throws SQLException {

        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        Statement stmt1 = conn.createStatement();
        ResultSet rs1 = stmt1.executeQuery(
                "SELECT ANUMBER,BNUMBER,DATETIME FROM CDR LIMIT '" + recordPerPage + "' OFFSET '" + offSet + "' ");
        while(rs1.next()) {
            x = rs1.getString("ANUMBER");
            y = rs1.getString("BNUMBER");
            z = rs1.getString("DATETIME");
            model.addRow(new Object[]{x, y, z});
        }

    }