• Register
2 votes
14 views

Problem:

I am beginner to jTable. I have already attached the jTable to JDBC database. But I don’t know how to retrieve them. My requirement is the saved data must be shown if I restart my program but currently it is not working for me.

I have written a jtable which has the 3 columns with the three column names are studentName, schoolName, joiningDate.

Please find below my jTable code

String mysql="SELECT * FROM studentList";
ResultSet myrs = myst.executeQuery(mysql);
while(myrs.next())
{
    String mystud = myrs.getString("studentName");
    String myshool = myrs.getString("schoolName");
    String myjoining = myrs.getString("joiningDate");
}

Can somebody solve my problem ?

8 5 2
3,230 points

1 Answer

0 votes

Solution:

 I have understood your problem. I have also used the same approach recently which you want to try so trying to help you. 

So the very first step to creation of the new MyTableModel as shown below

DefaultTableModel mymodel = new DefaultTableModel(new String[]{"Student Name ", " School Name ", " Joining Date "}, 0);

Next step is to load required data from your database as shown below

String mysql="SELECT * FROM studentList";
ResultSet myrs = myst.executeQuery(mysql);
Now you must add each row of required data to your table model as shown below
while(myrs.next())
{
    String mystud = myrs.getString("studentName");
    String myshool = myrs.getString("schoolName");
    String myjoining = myrs.getString("joiningDate");
    mymodel.addRow(new Object[]{mystud, myshool, myjoining });

}
Finally you must apply your model to the required JTable as shown below
table.setModel(mymodel);
I hope above solution will help you in fixing your issue.
8 4
5,680 points