jTable 连接到 MySQL 按钮错误
jTable connected to MySQL button error
我有以下 jframe :
我想让按钮工作,我对编程还是陌生的,有人可以帮助我吗?我希望添加行按钮向数据库添加新行,更新按钮让我保存更改并删除删除所选行,jTextBoxes 也连接到数据库
我试着这样做来更新:
Connection conn=null;
PreparedStatement pst = null;
try{
String value1=txt_cid.getText();
String value2=txt_carid.getText();
String value3=txt_aid.getText();
String value4=txt_rd.getText();
String value5=txt_bd.getText();
String value6=txt_bn.getText();
String sql="update booking set customer_id'"+value1+"',car_id'"+value2+"',agency_id'"+value3+"',return_date'"+value4+"',booking_date'"+value5+"',booking_number'"+value6+"',";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "table updated");
}catch(Exception e) {
JOptionPane.showMessageDialog(null,e);
}
但我没有成功,我收到异常错误
您没有说明错误是什么,但 UPDATE
对每个参数都采用等号运算符。还使用 PreparedStatement
占位符来避免 SQL Injection 攻击:
String sql = "update booking set customer_id=?, car_id=?,agency_id=?,return_date=?,booking_date=?,booking_number=?";
pst = conn.prepareStatement(sql);
pst.setInt(1, value1);
pst.setInt(2, value2);
... // set the other parameters
我有以下 jframe :
我想让按钮工作,我对编程还是陌生的,有人可以帮助我吗?我希望添加行按钮向数据库添加新行,更新按钮让我保存更改并删除删除所选行,jTextBoxes 也连接到数据库 我试着这样做来更新:
Connection conn=null;
PreparedStatement pst = null;
try{
String value1=txt_cid.getText();
String value2=txt_carid.getText();
String value3=txt_aid.getText();
String value4=txt_rd.getText();
String value5=txt_bd.getText();
String value6=txt_bn.getText();
String sql="update booking set customer_id'"+value1+"',car_id'"+value2+"',agency_id'"+value3+"',return_date'"+value4+"',booking_date'"+value5+"',booking_number'"+value6+"',";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "table updated");
}catch(Exception e) {
JOptionPane.showMessageDialog(null,e);
}
但我没有成功,我收到异常错误
您没有说明错误是什么,但 UPDATE
对每个参数都采用等号运算符。还使用 PreparedStatement
占位符来避免 SQL Injection 攻击:
String sql = "update booking set customer_id=?, car_id=?,agency_id=?,return_date=?,booking_date=?,booking_number=?";
pst = conn.prepareStatement(sql);
pst.setInt(1, value1);
pst.setInt(2, value2);
... // set the other parameters