enjeru
enjeru

Reputation: 55

How to show all records in database to JTable?

Excuse me. I want to show all records in JTable. But I get confused because my coding just show 1 record. When I click it again, that just show same records. Can you help me how to get all records?

try {

            Statement stmt;
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM menu");

            if (rs.next()) {
                String menu_id = rs.getString("menu_id");
                String menu_type = rs.getString("menu_type");
                String menu_cat = rs.getString("menu_cat");
                String menu_name = rs.getString("menu_name");
                String menu_price = rs.getString("menu_price");

                DefaultTableModel model = (DefaultTableModel) tblMenu.getModel();
                model.addRow(new Object[]{menu_id, menu_type, menu_cat, menu_name, menu_price});
            }

        } catch (Exception e) {
            System.out.println("Failed " + e);
        }

I have no Idea how to code it. Thank you

Upvotes: 1

Views: 1588

Answers (3)

Blasanka
Blasanka

Reputation: 22427

You have used if(rs.next()) you know that if condition checked if it is true or false.

rs.next()

shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise false. In combination with the if statement (instead of the while) this means that you expecting in only one row. simply,

rs.next()

check for next row if it is exist if condition passed and do the code block and get out from the block run the rest of the code.

As to the concrete question about iterate through the database table and add all to the jTable. You only have to change one keyword,

if to while

What happens when you use while keyword instead if keyword is that rs.next() check for next row and if it is true then condition true for while loop and go through the code block and when block is finish again check for next row. Likewise go through the all rows in your table and condition will fail after last row.

Upvotes: 0

user4624062
user4624062

Reputation:

Try this

        String[] cols={"id","type","cat","name","price"};
        ArrayList<String[]> list = new ArrayList<String[]>();
        while (rs.next()) {
            String menu_id = rs.getString("menu_id");
            String menu_type = rs.getString("menu_type");
            String menu_cat = rs.getString("menu_cat");
            String menu_name = rs.getString("menu_name");
            String menu_price = rs.getString("menu_price");
            list.add(new String[]{menu_id, menu_type, menu_cat, menu_name, menu_price});
        }

        String[][] elts= new String[list.size()][];
        elts = list.toArray(elts);
        JTable table = new JTable(elts,cols);
        JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        frame.getContentPane().add(pane);

Upvotes: 1

juergen d
juergen d

Reputation: 204746

Change

if (rs.next())

to

while (rs.next())

to run to the code block not just once, but till the end of the ResultSet.

It should actually look like this

DefaultTableModel model = (DefaultTableModel) tblMenu.getModel();
while (rs.next()) {
    String menu_id = rs.getString("menu_id");
    String menu_type = rs.getString("menu_type");
    String menu_cat = rs.getString("menu_cat");
    String menu_name = rs.getString("menu_name");
    String menu_price = rs.getString("menu_price");

    model.addRow(new Object[]{menu_id, menu_type, menu_cat, menu_name, menu_price});
}

Upvotes: 5

Related Questions