1

I'm trying to populate a JTable from a database, but the output is still empty. Here is my code :

    private void buttonsearchActionPerformed(java.awt.event.ActionEvent evt)
    {
        conn = DatabaseConnection.dbConnection();
        try {
            String Sql="select idp,nomp,prix,stock from produit where codep='" 
                       + textsearch.getText() + "'";
            pst = conn.prepareStatement(Sql);

            ResultSet rs = pst.executeQuery();
            DefaultTableModel model = new DefaultTableModel();
            Object[] columns = {"Id Produit", "Nom Produit", "Quantité", "Prix", "Stock"};
            model.setColumnIdentifiers(columns);
            table.setModel(model);
            Object[] row = new Object[5];

            if (rs.next())
            {
                row[0] = rs.getInt("idp");
                row[1] = rs.getString("nomp");
                //row[2] = rs.getString("");
                row[3] = rs.getString("prix");
                row[4] = rs.getString("stock");
                model.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }                        

table "poduit" is :

| idp | codep | nomp | prix | stock |

"Quantité" in

Object[] columns = {"Id Produit", "Nom Produit", "Quantité", "Prix", "Stock"}; 

It meant to modify numbers of items to create a billing.

my issue is the 2nd row is pasted right on the 1st one Thanks for help

Reporter
  • 3,897
  • 5
  • 33
  • 47
Fennec
  • 13
  • 4
  • 2
    You'd want to use a loop to iterate over the result set (you're currently just using the first entry) and add each result row to your model. And when doing that you'd of course need to create the table model and the column headers outside that loop. – Thomas May 06 '19 at 12:50
  • Additional to comment from Thomas : https://stackoverflow.com/questions/3549206/how-to-add-row-in-jtable – Reporter May 06 '19 at 12:54
  • @Thomas my code doesnt even show the 1st row,i dont know where's the problem – Fennec May 06 '19 at 13:03
  • 2
    Try debugging. What does `rs.next()` return? Also please do not use string concatenation for database queries. That's the basics to avoid SQL injections – Ferdz May 06 '19 at 13:04
  • I second Ferdz' comment. Debug your code and see whether the query returns anything. And let me tell you what could happen if you append parameters into the query string instead of using a `PreparedStatement` and `setString(...)` (or any of the other methods to set parameters) - [the tale of bobby tables](https://xkcd.com/327/). – Thomas May 06 '19 at 13:09
  • @Ferdz just tried to print my resultset result is empty...altho my query is fine :/ – Fennec May 06 '19 at 13:13
  • Why are you doing if(rs.next()) instead of while(rs.next()) ?? – Antoniossss May 06 '19 at 13:34
  • @Fennec 1st: Have you read my posted link? 2nd: Where is the 'table' object? – Reporter May 06 '19 at 14:06
  • @Fennec according my posted link there is a code line I misse in your posted code. The line is `DefaultTableModel model = (DefaultTableModel) table.getModel();`. Btw. can you modify your posted code so, that your example is runnable out of the box? – Reporter May 06 '19 at 15:04
  • @reporter thanks bro that line solved my problem ^^ – Fennec May 06 '19 at 15:29
  • @Fennec Can I cast my comment into an answer and you mark it as accepted? – Reporter May 06 '19 at 20:49
  • @reporter sure. – Fennec May 07 '19 at 10:00

1 Answers1

0

According How to add row in JTable? and the Internet itself, all examples uses following line:

DefaultTableModel deFaultTableModel = (DefaultTableModel) myJTable.getModel();

so you have to replace the code line 'DefaultTableModel model = new DefaultTableModel();' by DefaultTableModel model = (DefaultTableModel) table.getModel();.

Reporter
  • 3,897
  • 5
  • 33
  • 47