Retrieving Data from JDBC Database into Jtable

后端 未结 6 1666
长情又很酷
长情又很酷 2020-12-16 07:57

Hi I have successfully linked my jTable to JDBC database. However, I am having trouble retrieving them. I want the saved data to appear when I restart the program but it is

6条回答
  •  时光取名叫无心
    2020-12-16 08:41

    Start by creating a new TableModel...

    DefaultTableModel model = new DefaultTableModel(new String[]{"Class Name", "Home work", "Due Date"}, 0);
    

    Load the data from the database...

    String sql="SELECT * FROM hwList";
    ResultSet rs = st.executeQuery(sql);
    

    Add each row of data to the table model...

    while(rs.next())
    {
        String d = rs.getString("className");
        String e = rs.getString("homeWork");
        String f = rs.getString("dueDate");
        model.addRow(new Object[]{d, e, f});
    }
    

    Apply the model to your JTable...

    table.setModel(model);
    

    You may also want to look at The try-with-resources Statement and make sure you're managing your resources properly

提交回复
热议问题