How to populate JTable from database on button event

前端 未结 2 984
误落风尘
误落风尘 2021-01-17 02:27

I have a text box that allows users to put in select type queries with the idea that when they click a button the result of the select statement will be shown in a JTable. I

2条回答
  •  [愿得一人]
    2021-01-17 03:30

    Here is my solution to your problem. I have retained your variable names though not all of them followed Java conventions. I think all you wanted was that the user would type SQL query in JTextArea and see the result of the query in a JTable.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.table.AbstractTableModel;
    
    public class Console {
       private JFrame frame; 
       private JTextArea textAreaQuery; 
       private JTable table_ResQues;
       private JButton btnNewButton;
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Console window = new Console();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public Console() {
        initialize();
    }
    
    private void initialize() {
        frame = new JFrame("SQL Query");
        frame.setBounds(100, 100, 950, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        textAreaQuery = new JTextArea();
        btnNewButton = new JButton("Execute");
        table_ResQues = new JTable();
    
        JPanel queryPanel=new JPanel(new BorderLayout()); // holds JTextArea and JButton
    
        queryPanel.add(new JScrollPane(textAreaQuery), BorderLayout.CENTER);
    
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        btnPanel.add(btnNewButton);
        queryPanel.add(btnPanel, BorderLayout.SOUTH);       
    
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, queryPanel,
                new JScrollPane(table_ResQues));
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(150);
        frame.setContentPane(splitPane);        
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                /*try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e2) {                   
                    e2.printStackTrace();
                }*/
                String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/Physiology.mdb;";         
                Connection conn = null;
                Statement st = null;
                try {
                    conn = DriverManager.getConnection(connectDB);
                    //conn = DriverManager.getConnection("jdbc:mysql://localhost/cwcx", "root", "admin");// MySQL connection
                    st = conn.createStatement();
                    System.out.println("Connection To Database Made");
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
    
                String myquery = textAreaQuery.getText();
                if(myquery.endsWith(";")){
                    myquery=myquery.substring(0, myquery.length()-1);
                }
                ResultSet rs = null;
                try {
                    rs = st.executeQuery(myquery);
                    // extract column information
                    ResultSetMetaData rsmd = rs.getMetaData(); 
                    int columnCount = rsmd.getColumnCount();
                    List columnData = new ArrayList(columnCount);
                    for (int i = 1; i <= columnCount; i++) {
                        columnData.add(rsmd.getColumnName(i));
                    }
                    // sql result data
                    List> rowData = new ArrayList>();
                    while (rs.next()) {
                        List row = new ArrayList(columnCount);
                        for (int i = 0; i < columnCount; i++) {
                            row.add(rs.getObject(i + 1));
                        }
                        rowData.add(row);
                    }                   
                    table_ResQues.setModel(new ListTableModel(rowData, columnData));                    
                } catch (SQLException e1) {
                    JOptionPane.showMessageDialog(frame, e1.getMessage(), "SQL Exception", JOptionPane.ERROR_MESSAGE);
                    e1.printStackTrace();
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e1) {                         
                            e1.printStackTrace();
                        }
                    }
                    if(st!=null){
                        try {
                            st.close();
                        } catch (SQLException e1) {                         
                            e1.printStackTrace();
                        }
                    }
                    if(conn!=null){
                        try {
                            conn.close();
                        } catch (SQLException e1) {                         
                            e1.printStackTrace();
                        }
                    }
                }
            }
    
        });
    
    }
    
    // this table model is created from two dimensional List rowData and single dimensional List columnData
    private static class ListTableModel extends AbstractTableModel{     
        private static final long serialVersionUID = 1L;        
        private List> rowData;     
        private List columnData;        
        public ListTableModel(List> rowData, List columnData) {            
            this.rowData = rowData;
            this.columnData = columnData;
        }
    
        @Override
        public int getRowCount() {          
            return rowData.size();
        }
    
        @Override
        public int getColumnCount() {           
            return columnData.size();
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {       
            return rowData.get(rowIndex).get(columnIndex);
        }
    
        @Override
        public String getColumnName(int column) {           
            return columnData.get(column);
        }
    
        @Override
        public Class getColumnClass(int columnIndex) {
            Object obj=rowData.get(0).get(columnIndex);
            return obj.getClass();
        }       
      }
    
    }
    
    
    

    On my system, I have tested it with MySQL database. But I have commented out that part. You may try this on your system without any modification. Please do tell me whether you wanted to achieve this solution or not.

    提交回复
    热议问题