I,ve set up a JTable and want to populate it from an Access database useing jdbc odbc.

The data returned is placed into an array but my problem is how to use the array results with...

jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    new Object [][] {
                        {list[0], list[1], list[2], list[3], list[4]},
                    },
                    new String [] {
                        "Product", "Price", "Stock", "Code", "Amount Sold"
                    }
                ));

full code as follows...

 private void testtableActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        table.setBounds(0,0,525,550);
        table.setLocationRelativeTo(null);
        table.setVisible(true);
        int rowCount =getRow();
        Object[][] list = new Object [rowCount][5];
        jTable1 = new javax.swing.JTable();

  // connect to database
            String data = "jdbc:odbc:SNbase";
       try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(
                data, "", "");
            Statement st = conn.createStatement();
            // set sql query
            ResultSet rec = st.executeQuery(
                    "SELECT * " +
                    "FROM stock_items");

             while(rec.next()) {
                    for (int i=0; i<rowCount; i++){
                    // place query result into array
                    list[i][0] = rec.getString(2);
                    double num = Double.parseDouble(rec.getString(3));
                    list[i][1] = "£ " + num;
                    list[i][2] = rec.getString(4);
                    list[i][3] = rec.getString(5);
                    list[i][4] = rec.getString(7);
                 }
           }
                  st.close();

            } catch (SQLException s) {
                System.out.println("SQL Error: " + s.toString() + " "
                    + s.getErrorCode() + " " + s.getSQLState());
            } catch (Exception e) {
                System.out.println("Error: " + e.toString()
                    + e.getMessage());
            }

                jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    new Object [][] {
                        {list[0], list[1], list[2], list[3], list[4]},
                    },
                    new String [] {
                        "Product", "Price", "Stock", "Code", "Amount Sold"
                    }
                ));



jScrollPane1.setViewportView(jTable1);
// end method
    }

First set the model like below

setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
           "Product", "Price", "Stock", "Code", "Amount Sold"
                })

try the below code to add data to the table

model = (DefaultTableModel) jTable1.getModel();

......
Object[] obj = {list[0], list[1], list[2], list[3], list[4]};
model.addRow(obj);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.