• Register
2 votes
23 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

Please log in or register to answer this question.

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 1
5,680 points

Related questions

0 votes
1 answer 13 views
0 votes
1 answer 14 views
0 votes
1 answer 47 views
0 votes
1 answer 27 views
0 votes
1 answer 26 views
0 votes
1 answer 11 views
11 views
Problem: I need help: how to retrieve data from database of a particular user after login in php
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
2 answers 425 views
0 votes
1 answer 21 views
21 views
I need assistance in javascript variable value. specificalli i am seeking for passing the variables within the same page.
asked Sep 14, 2020 Daniel Anderson 4k points