Most simple code to populate JTable from ResultSet

前端 未结 10 985
慢半拍i
慢半拍i 2020-11-22 11:22

I googled the whole day and no luck. I call getnPrintAllData() method after pressing OK button. So the code is:

public class DatabaseSQLiteConne         


        
10条回答
  •  深忆病人
    2020-11-22 11:58

    I think the simplest way to build a model from an instance of ResultSet, could be as follows.

    public static void main(String[] args) throws Exception {
        // The Connection is obtained
    
        ResultSet rs = stmt.executeQuery("select * from product_info");
    
        // It creates and displays the table
        JTable table = new JTable(buildTableModel(rs));
    
        // Closes the Connection
    
        JOptionPane.showMessageDialog(null, new JScrollPane(table));
    
    }
    

    The method buildTableModel:

    public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {
    
        ResultSetMetaData metaData = rs.getMetaData();
    
        // names of columns
        Vector columnNames = new Vector();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }
    
        // data of the table
        Vector> data = new Vector>();
        while (rs.next()) {
            Vector vector = new Vector();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
    
        return new DefaultTableModel(data, columnNames);
    
    }
    
    
    

    UPDATE

    Do you like to use javax.swing.SwingWorker? Do you like to use the try-with-resources statement?

    public class GUI extends JFrame {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new GUI().setVisible(true);
                }
            });
        }
    
        private final JButton button;
        private final JTable table;
        private final DefaultTableModel tableModel = new DefaultTableModel();
    
        public GUI() throws HeadlessException {
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            table = new JTable(tableModel);
            add(new JScrollPane(table), BorderLayout.CENTER);
    
            button = new JButton("Load Data");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new SwingWorker() {
                        @Override
                        protected Void doInBackground() throws Exception {
                            loadData();
                            return null;
                        }
                    }.execute();
                }
            });
            add(button, BorderLayout.PAGE_START);
    
            setSize(640, 480);
        }
    
        private void loadData() {
            LOG.info("START loadData method");
    
            button.setEnabled(false);
    
            try (Connection conn = DriverManager.getConnection(url, usr, pwd);
                    Statement stmt = conn.createStatement()) {
    
                ResultSet rs = stmt.executeQuery("select * from customer");
                ResultSetMetaData metaData = rs.getMetaData();
    
                // Names of columns
                Vector columnNames = new Vector();
                int columnCount = metaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    columnNames.add(metaData.getColumnName(i));
                }
    
                // Data of the table
                Vector> data = new Vector>();
                while (rs.next()) {
                    Vector vector = new Vector();
                    for (int i = 1; i <= columnCount; i++) {
                        vector.add(rs.getObject(i));
                    }
                    data.add(vector);
                }
    
                tableModel.setDataVector(data, columnNames);
            } catch (Exception e) {
                LOG.log(Level.SEVERE, "Exception in Load Data", e);
            }
            button.setEnabled(true);
    
            LOG.info("END loadData method");
        }
    
    }
    
        

    提交回复
    热议问题