正如标题所述,我正在尝试编辑现有(本地)MySQL 服务器中的字段。添加和删除行就像一个魅力,但是当我尝试使用此代码编辑它们时:
private void EditActionPerformed(java.awt.event.ActionEvent evt) {
try{
String edit1 = lNaam.getText();
String edit2 = lEmail.getText();
String edit3 = lAdres.getText();
String edit4 = lPlaats.getText();
String edit5 = lPostcode.getText();
String edit6 = lTelefoon.getText();
String edit7 = lBedrijfsNaam.getText();
String sql="update customers set naam='"+edit1+"',email='"+edit2+"',adres='"+edit3+"',plaats='"+edit4+"',postcode='"+edit5+"',telefoon='"+edit6+"',bedrijfsnaam='"+edit7+"'where KL_NR";
preparedStatement=connect.prepareStatement(sql);
preparedStatement.execute();
JOptionPane.showMessageDialog(null, "Updated");
}catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
编辑:我忘了说这个,KL_NR 字段是一个 AUTO_INCREMENT 字段,应该保持不变,我不知道该放什么
它不断编辑表中的所有行,而不是仅编辑 1 行。
此代码用于选择行、删除等:
private void Table_customerMouseReleased(java.awt.event.MouseEvent evt) {
try{
int row = Table_customer.getSelectedRow();
String TClick = (Table_customer.getModel().getValueAt(row, 0).toString());
String sql="select * from customers where KL_NR='"+TClick+"'";
preparedStatement=connect.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
if(resultSet.next()){
String add1 =resultSet.getString("naam");
lNaam.setText(add1);
String add2 =resultSet.getString("Email");
lEmail.setText(add2);
String add3 =resultSet.getString("Adres");
lAdres.setText(add3);
String add4 =resultSet.getString("Plaats");
lPlaats.setText(add4);
String add5 =resultSet.getString("Postcode");
lPostcode.setText(add5);
String add6 =resultSet.getString("Telefoon");
lTelefoon.setText(add6);
String add7 =resultSet.getString("Bedrijfsnaam");
lBedrijfsNaam.setText(add7);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
这是我的删除代码:
private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
String sql ="delete from test.customers where NAAM=? ";
try {
// TODO add your handling code here:
preparedStatement = connect.prepareStatement(sql);
preparedStatement.setString(1, lNaam.getText());
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "Deleted");
} catch (SQLException ex) {
Logger.getLogger(DataView.class.getName()).log(Level.SEVERE, null, ex);
}
Update_table();
}
我希望你能帮助我。:S