0

I am trying to populate a java database with car brands, models, and if they are available but I have a big issue. When I enter multiple cars to the table, the new car prints to the table, but previous cars I have entered also reprint [Issue.png][1]. So first I entered Ferrari Enzo, then I entered Tesla S but Ferrari Enzo is repeating.

I know the issue is not with adding the cars to the database because if I look in the database it only shows the two I entered [DatabasePic.png][2]. I know my problem is with how I am entering my cars into the JTable. My methods are below..

ButtonClickEvent:

private void refreshbtnMouseClicked(java.awt.event.MouseEvent evt) {                                        
    try {
        String Brand = brandlbl.getText();
        String Model = modellbl.getText();
        Boolean Available = false;
        switch (availabilitybox.getSelectedItem().toString()) {
            case "True":
                Available = true;
                break;
            case "False":
                Available = false;
                break;
        }
        InsertApp nw = new InsertApp();
        nw.insert(Brand, Model, Available);
        nw.refreshDatabase(jTable1);
    } catch (SQLException ex) {
        Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Insert Method:

public void insert(String brand, String model, Boolean available) {
    String sql = "INSERT INTO CARDATA(brand,model,available) VALUES(?,?,?)";

    try (Connection conn = this.connect();
            PreparedStatement pstmt = conn.prepareStatement(sql)) {

        pstmt.setString(1, brand);
        pstmt.setString(2, model);
        pstmt.setBoolean(3, available);

        pstmt.executeUpdate();
    } catch (SQLException e) {

        System.out.println(e.getMessage());
    }

}

Refreshing my database:

public void refreshDatabase(JTable table) throws SQLException {
    Connection con = connect();
    String SQL = "SELECT * FROM CARDATA";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(SQL);
    int col = rs.getMetaData().getColumnCount();
    System.out.println(col);
    while (rs.next()) {
        Object[] rows = new Object[col];
        for (int i = 1; i <= col; i++) {
            rows[i - 1] = rs.getObject(i);
        }
        ((DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, rows);
    }
    con.close();
    rs.close();
    stmt.close();
}

1 Answer 1

1

When I enter multiple cars to the table, the new car prints to the table, but previous cars I have entered also reprint

You need to clear the data in the TableModel before you start adding data back into it.

The basic code should be something like:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.setRowCount(0);

while (rs.next()) 
{
    ...

    model.addRow(...);
}

Or instead of retrieving all the data from the database every time you do an insert, just add the data to the table at the same time:

pstmt.executeUpdate();
model.addRow(...);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.