Connection con=DriverManager.getConnection(url, user, pass);
if (con!= null) {
System.out.println("Connected");
}
String sql="select product_name, price from inventorytable where product_code=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,ProductCode.getText());
ResultSet rs= pst.executeQuery();
ResultSetMetaData rsmetadata=rs.getMetaData();
int columns=rsmetadata.getColumnCount();
DefaultTableModel dtm=new DefaultTableModel();
Vector columns_name=new Vector();
Vector data_rows=new Vector();
for(int i=1; i<=columns;i++){
columns_name.addElement(rsmetadata.getColumnName(i));
}
dtm.setColumnIdentifiers(columns_name);
while(rs.next()){
data_rows=new Vector();
for(int j=1;j<=columns;j++){
data_rows.addElement(rs.getString(j));
}
dtm.addRow(data_rows);
}
BillTable.setModel(dtm);
}
我想从数据库中添加具有匹配用户输入的产品代码的行到jatable。这段代码的作用是添加thr行,但是当我尝试进入另一行时,它会替换前一行并添加新行..我应该在此代码中做出哪些更改才能实现。