我在代码中做了2种不同的方式,但两种方式都不适合我。 您能帮我解决这个问题吗?
第一种方式:
private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {
id=idField.getText();
newpass1=newPass1.getText();
newpass2=newPass2.getText();
try {
con = DriverManager.getConnection("jdbc:derby://localhost:1527/hj", "xxx", "xxx");
st = con.createStatement();
if (newpass1.equals(newpass2)){
ResultSet rs = st.executeQuery("update patient set patient_Password="+ newpass1 +" where patient_Id="+id+" and patient_Id like '200%'");
JOptionPane.showMessageDialog(this , "Successfully changed", "Patient password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
ResultSet rs1 = st.executeQuery("update Nurse set nurse_password="+ newpass1 +" where nurse_id="+id+" and nurse_id like '102%'");
JOptionPane.showMessageDialog(this , "Successfully changed", "Nurse password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
ResultSet rs2 = st.executeQuery("update doctor set doctor_password="+ newpass1 +" where doctor_id="+id+" and doctor_id like '101%'");
JOptionPane.showMessageDialog(this , "Successfully changed", "Doctor password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
} else
JOptionPane.showMessageDialog(this , "Not equal", "Your new passwords are not equal!! , try again",JOptionPane.ERROR_MESSAGE );
}catch (Exception x){
JOptionPane.showMessageDialog(this, x.getStackTrace());
}
}
第二种方式:
private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {
id=idField.getText();
newpass1=newPass1.getText();
newpass2=newPass2.getText();
try {
con = DriverManager.getConnection("jdbc:derby://localhost:1527/hj", "xxx", "xxx");
st = con.createStatement();
if (newpass1.equals(newpass2)){
if (id.startsWith("200")){
ResultSet rs = st.executeQuery("update patient set patient_Password="+ newpass1 +" where patient_Id="+id+"");
JOptionPane.showMessageDialog(this , "Successfully changed", "Patient password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
}
else if (id.startsWith("102")){
ResultSet rs = st.executeQuery("update Nurse set nurse_password="+ newpass1 +" where nurse_id="+id+"");
JOptionPane.showMessageDialog(this , "Successfully changed", "Nurse password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
}
else if (id.startsWith("101")){
ResultSet rs = st.executeQuery("update doctor set doctor_password="+ newpass1 +" where doctor_id="+id+"");
JOptionPane.showMessageDialog(this , "Successfully changed", "Doctor password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
}
} else
JOptionPane.showMessageDialog(this , "Not equal", "Your new passwords are not equal!! , try again",JOptionPane.ERROR_MESSAGE );
}catch (Exception x){
JOptionPane.showMessageDialog(this, x.getStackTrace());
}
}
答案 0 :(得分:2)
请使用$responstable
!
PreparedStatement
通过串联查询,您将得到 if (id.startsWith("200")){
try (PreparedStatement pstmt = conn.prepareStatement("UPDATE patient SET patient_passwort=? WHERE patient_id=?");) {
pstmt.setString(1, newpass1);
pstmt.setString(2, id);
int rows = pstmt.executeUpdate();
JOptionPane.showMessageDialog(this , "Successfully changed",
"Patient password successfuly changed! (updated rows: "+rows+")", JOptionPane.PLAIN_MESSAGE);
}
}
。
未引用新密码(此处为update patient set patient_Password=abcdefghi where patient_Id=200340 and patient_Id like '200%'
),这对于查询中的字符串是必需的。 abcdefghi
也未加引号,但可能是数字字段,不必加引号。
顺便说一句:
不需要查询部分patient_id
。
您应该关闭任何PreparedStatement / Statement实例,这可以通过使用try-with-resources(patient_id like '200%'
)来完成。 try (PreparedStatement xxx = ...) { ... your code } // closes automatically
和Connection
也是如此。
由于ResultSet
是整数,因此您可能需要这样使用它:id
提示:
如果使用Apache commons-dbutils,您将更加轻松自在。例如int updId = Integer.parseInt(id); ... pstmt.setInt(2, updId); ...
org.apache.commons.dbutils.QueryRunner