Title: Record is not loaded from the database on the JTable java netbeans
Tags: java
Question: I am trying to display the records on JTable when the frame is loaded, but the records didn't display. This is what I've tried so far:
public void load()
try {
Connection con1;
PreparedStatement insert;
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/javapos","root","");
insert = con1.prepareStatement("SELECT name,status FROM category");
ResultSet Rs = insert.executeQuery();
while(Rs.next()){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
String name = Rs.getString("name");
String status = Rs.getString("status");
model.addRow(new Object[]{name,status });
}
jTable1.setModel(model);
} catch (Exception e) {
System.out.println("Failed " + e);
}
Comment: could be `ResultSet Rs` doestn't have any row/data. is your query return any data?

Comment: Your sample include the model, the view and the controler. You might want to debug this to see what is happening... and it won't compile since you are using `model` outside of the block where it was declared... Please propose a [mcve]

Comment: what is displayed instead of the data?

Comment: Try do add a few hardcoded rows instead of loading them from the database.

Comment: Do something like this: `model.addRow(new Object[]{"name1","status1" });`

Comment: BTW: the line `jTable1.setModel(model);` is useless since `model` is obtained from the table.

Comment: blank table displayed no error displayed

Comment: i don't how to do the thing i am totally beginner of java

Comment: model.addRow(new Object[]{"name1","status1" }); i tried this sir. records is not loaded on the table

Here is the accepted answer: My guess since your code should not compile but seems to (you are seing an empty JTable after all).
You have declare a DefaultTableModel model before the snippet.
How do I know that? With the code you proposed jTable1.setModel(model); can't compile because model doesn't exist in the scope (outside of the loop).
while(Rs.next()){
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
String name = Rs.getString("name");
String status = Rs.getString("status");
model.addRow(new Object[]{name,status });
jTable1.setModel(model);
Compile error :
"model" cannot be resolved to a variable.
I would guess you have declare it somewhere before. In the loop you declare a new one in a different scope, hiding the original one. In this last model instance, you insert your rows.
But after the loop, you set the first model that have no row in the jtable.
A basic example of what is happening, here I will rename both instance but in your code, model1 and model2 are named model.
model1 = new Model();
while(rs.next()){
model2 = new Model();
model2.addRow(...);
table.setModel(model1);
Quick correction, remove the declaration in the loop.
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
while(Rs.next()){
//DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
String name = Rs.getString("name");
String status = Rs.getString("status");
model.addRow(new Object[]{name,status });
jtable1.setModel(model);
Declare one TableModel
Comment for this answer: With this, you are appending the value to the existing one. If you want, you can first clean the model using a solution from [Java - how to clear JTable](https://stackoverflow.com/questions/4577792/how-to-clear-jtable)
Comment for this answer: it is working sir.but when i add the record and refreshed data should as duplicate values

Here is another answer: I would suggest you to make a separate two dimensional array and after retrieving data from Database save that data into that two dimensional array and after that you can assign that two dimensional array to the Jtable Model.
Comment for this answer: can you write the code for that it easy to understand please

Here is another answer: I can't reproduce exactly what you're trying to do, but I've tryed this:
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{"name1","status1" });
model.addRow(new Object[]{"name2","status2" });
model.addRow(new Object[]{"name3","status3" });
and the three rows are indeed added at the end of the table. Are you sure you have any data in your database table? could an exception being thrown?
UPDATE
I suspect that you have not defined the columns in the model:
UPDATE 2: Reset model before adding rows
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int r = model.getRowCount();
while (r &gt; 0) {
model.removeRow(--r);
ResultSet Rs = insert.executeQuery();
while(Rs.next()){
String name = Rs.getString("name");
String status = Rs.getString("status");
model.addRow(new Object[]{name,status });
Comment for this answer: @ProgrammerHari what's your table model?
Comment for this answer: @ProgrammerHari you may need to clear the table's content before you fill it. See update 2
Comment for this answer: i didn't get and data from database
Comment for this answer: yes i defined the column . it is working but data duplicated.data show twice

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 9, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:01/15/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java>', 'Title': 'Record is not loaded from the database on the JTable java netbeans', 'ViewCount': 151, 'id': 54193850, 'user_AboutMe': None, 'user_AccountId': 14762519.0, 'user_DisplayName': 'Programmer Hari', 'user_Reputation': 181.0, 'user_Views': 93.0, 'user_id': 10661421.0}}