[英]java - jTable database repeating values
I am trying to populate a java database with car brands, models, and if they are available but I have a big issue. 我试图用汽车品牌,型号填充Java数据库,如果可用,但我遇到了很大的问题。 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]. 当我在桌子上输入多辆车时,新车会打印到桌子上,但是我输入的以前的车也会重新打印[Issue.png] [1]。 So first I entered Ferrari Enzo, then I entered Tesla S but Ferrari Enzo is repeating. 因此,我首先进入法拉利恩佐(Ferrari Enzo),然后进入特斯拉S(Tesla S),但法拉利恩佐(Ferrari Enzo)在重复。
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]. 我知道问题不在于将汽车添加到数据库中,因为如果我查看数据库,它只会显示我输入的两个[DatabasePic.png] [2]。 I know my problem is with how I am entering my cars into the JTable. 我知道我的问题是我如何将汽车输入JTable。 My methods are below.. 我的方法如下。
ButtonClickEvent: 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();
}
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. 在开始向其添加数据之前,需要清除TableModel中的数据。
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(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.