Most simple code to populate JTable from ResultSet

前端 未结 10 855
慢半拍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:52

    With all factors put into consideration, with regard to code architecture using modular programming would work very well and with more simplicity in the code. Write a simple

    getData() function @returns a 2D array of the data from the database.
    

    pass this funtion to the constructor of the JTabel() constructor i.e

    JTabel myTable = new JTable(getData(),columnsArray);
    

    In this case the second argument: columnsArray is a single dimensional array that has the column names

    String[] columns = {{"ID","DataOfBirth","Age","Grade","Marks","RegNumber"}};
    

    pass the JTable object to a JScrollPane then you are done, right after adding the ScrollPane to the container ofcourse

    JScrollPane scrollPane = new JScrollPane(myTable);
    
    JFrame myFrame = new JFrame();
    myFrame.add(scrollPane);
    

    Here is a sample function getData() that queries the database for the data that later is passed on to the JTable

    public String[][] getRecords() {
        try (Connection conect = DriverManager.getConnection("jdbc:mysql://localhost:3306/online_students_registration", "root", "")) {
            Statement stm = conect.createStatement();
            String SELECT_QUERY = "SELECT COUNT(*) FROM medicalrecords ;";
            ResultSet cursor = stm.executeQuery(SELECT_QUERY);
            while (cursor.next()) {
                rows = cursor.getInt("COUNT(*)");
                System.out.println("Table will have " + rows + " Rows");
            }
            System.err.println("Contacts row count is obtained!!");
            if (rows < 1) {
                JOptionPane.showMessageDialog(null, "There is NO DATA");
                //contactsRowsCount = 1;
                //System.out.println("Table rows succefully reset to  " + contactsRowsCount + " Rows");
                dataValues = new String[1][8];
                //dataValues[1][5] = "No Values";
    
                for (int i = 0; i < 1; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (j == 0) {
                            dataValues[i][j] = "No Details Available";
                            System.out.println("" + dataValues);
                        } else {
                            dataValues[i][j] = "...";
                            System.out.println("Contacts" + dataValues[i][j]);
                        }
                    }
                }
    
                System.out.println("Return statement is being executed on 0 rows ");
                //return doctoredDataValues;
            } else if (rows > 0) {
    
                System.out.println("obtain contacts code is being run under  " + rows + " Rows");
                dataValues = new String[rows][8];    //declare array for contacts table data
                System.out.println("[ Line 1584 ]The dataValues object for the JTable succefully set");
    
                String SELECT_QUERY_CONTACT = "SELECT * FROM medicalrecords; ";
    
                //OBTAIN CONTACTS FROM DB WITH REGARD TO CONTACT CATEGORY SPECIFIED
                ResultSet contactsTableCursor = stm.executeQuery(SELECT_QUERY_CONTACT);
                //use iterator-algorithm to insert values into the JTable
                for (int i = 0; contactsTableCursor.next() && i < rows; i++) {
                    for (int j = 0; j < 8; j++) {
                        dataValues[i][j] = contactsTableCursor.getString(j + 1);
                        System.out.println("Contacts" + dataValues[i][j]);
                    }
                }
            }
            JOptionPane.showMessageDialog(null, "Medical Details Added Succefully!!");
        } catch (SQLException e) {
    
            JOptionPane.showMessageDialog(null, "Unable to Obtain contacts:Server is Offline(LINE 1568)" + e.getMessage());
        }
    
        return dataValues;
    
    }
    

提交回复
热议问题