
[英]How to match unique ID from the different table in database and retrieve data according to that unique ID in Java?
[英]change password from database according to different id beginnings
我有 3 个表(医生、护士、患者),它们都有不同的 id 开头,医生 id 以 101 开头,护士以 102 开头,患者以 200 开头。 我想根据他们 id 的开头更改密码。 在我的 JFrame 中,我有 5 个 JComponents、4 个 Jtextfields、1 个 Jbutton 1 个 Jtextfields 用于 id(名称:idField) 1 个 Jtextfields 用于当前密码(名称:currentPass) 2 个 Jtextfields 用于新密码(名称:newPass1、newPass2) 1 个 Jbutton 用于动作(名称:changeButton)
我在我的代码中做了 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());
}
}
一定要使用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
也没有被引用,但它可能是一个数字字段,不需要被引用。
顺便提一句:
patient_id like '200%'
的查询部分patient_id like '200%'
。
您应该关闭任何 PreparedStatement/Statement 实例,这可以通过使用 try-with-resources 来完成( try (PreparedStatement xxx = ...) { ... your code } // closes automatically
)。 Connection
和ResultSet
。
因为id
是一个整数,你可能想这样使用它: int updId = Integer.parseInt(id); ... pstmt.setInt(2, updId); ...
int updId = Integer.parseInt(id); ... pstmt.setInt(2, updId); ...
Tipp:如果您使用 Apache commons-dbutils,您的生活会更加轻松。 例如org.apache.commons.dbutils.QueryRunner
QueryRunner r = new QueryRunner();
int rows = r.update(conn,
"UPDATE patient SET patient_passwort=? WHERE patient_id=?",
newpass1, id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.