1

So I was recently trying to insert data into a Jtable from a database. The program runs a "Quote" Jframe which contains the JTable called "tblQuote". one the Quote screen there is a button called "Add product" which opens a Frame that allows the user to select a product from the database. I added a method in the class quote to update the table which receives the values as a parameter from the add product class. The add product class receives the values from my working class that connects to the database and handles all the queries.

Please not that all the values are successfully brought in from the database and that the method to update the table has been proved to run by the System.out.println that I used to test if it ran and if values were correct

but the still won't display in the table.

Database connection code:

    public String updateTableQ(String name) {
    String results = "";
    try {
        String qry = "SELECT * FROM tblProducts WHERE Name =?";
        PreparedStatement pst = conn.prepareStatement(qry);
        pst.setString(1, name);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            results += rs.getString("Excl Price") + "/"
                    + rs.getString("Incl Price") + "/";
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    return results;
}

}

update table code:

    public void updateTable(String name, double ex, int qty, double vat, double disc, double total) {
    Object[] row = {name, ex, qty, vat, disc, total};
    DefaultTableModel model = (DefaultTableModel) tblQuote.getModel();
    model.addRow(row);
    System.out.println(name + " " + ex + " " + qty + " " + vat + " " + disc + " " + total);
}

}

Calling methods in the add class:

    String name = cmbProduct.getSelectedItem().toString();
    String str =db.updateTableQ(name);
    String[] arr = str.split("/");
    double excl = Double.parseDouble(arr[0]);
    double total = Double.parseDouble(arr[1]);
    double vat = 15;
    double disc = Double.parseDouble(txtInDiscount.getText());
    int qty = Integer.parseInt(txtInQty.getText());
    q.updateTable(name, excl, qty, vat, disc, total);
    this.dispose();
Jarryd vn
  • 31
  • 6

0 Answers0