public Modife queryVerify(Modife modifeModel) { String id = modifeModel.getID(); String pw = modifeModel.getPassword(); String npw = modifeModel.getnPassword(); String sqlStr ="select id,password from register where "+ "id = ? and password = ?"; try { preSql = con.prepareStatement(sqlStr); preSql.setString(1,id); preSql.setString(2,pw); rs = preSql.executeQuery(); if(rs.next()==true) { modifeModel.setLoginSuccess(true); JOptionPane.showMessageDialog(null,"登录成功", "恭喜",JOptionPane.WARNING_MESSAGE); } else { modifeModel.setLoginSuccess(false); JOptionPane.showMessageDialog(null,"登录失败", "登录失败,重新登录",JOptionPane.WARNING_MESSAGE); } con.close(); } catch(SQLException e) {} return modifeModel; } } 如果if语句判断成功后怎么才能将第二行的值pw修改为npw?
时间: 2024-02-14 13:04:21 浏览: 20
可以在if语句块中添加修改密码的代码,如下:
```
if(rs.next()==true) {
preSql = con.prepareStatement("UPDATE register SET password=? WHERE id=?");
preSql.setString(1, npw);
preSql.setString(2, id);
preSql.executeUpdate();
modifeModel.setLoginSuccess(true);
JOptionPane.showMessageDialog(null,"登录成功","恭喜",JOptionPane.WARNING_MESSAGE);
}
```
其中,我们新建了一个PreparedStatement对象,使用UPDATE语句将指定id的密码修改为npw,并使用setString方法设置参数值,最后使用executeUpdate方法执行更新操作。注意,这里的id需要是登录成功后的id。
相关问题
public String getId(String id){this .id=id;}
这段代码有语法错误。应该修改为:
```
public void setId(String id){
this.id = id;
}
```
这是一个setter方法,用于给类中的id属性赋值。修正后的代码中,参数id将被赋值给类中的id属性。需要注意的是,setter方法的返回值通常为void,因为它不需要返回任何值。
else if (e.getSource().equals(login)) { String id=jt1.getText(); String username = jt2.getText(); String password = String.valueOf(jpf.getPassword()); // 遍历所有学生信息,判断输入的用户名和密码是否匹配 boolean isMatched = false; for (Student student : students) { if (username.equals(student.getUserName()) && password.equals(String.valueOf(student.getPassword()))&&id.equals(student.getId())) { isMatched = true; break; } }修改代码
在对所有学生信息进行遍历之前,您可以先进行一些输入验证,比如判断输入框是否为空。另外,您可以把判断逻辑封装成一个方法,以提高代码可读性和复用性。修改后的代码如下所示:
```
else if (e.getSource().equals(login)) {
String id = jt1.getText();
String username = jt2.getText();
String password = String.valueOf(jpf.getPassword());
// 输入验证
if (id.isEmpty() || username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入完整信息!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
// 判断用户名、密码和ID是否匹配
boolean isMatched = isStudentMatched(id, username, password);
if (isMatched) {
JOptionPane.showMessageDialog(this, "登录成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "用户名、密码或ID错误!", "提示", JOptionPane.WARNING_MESSAGE);
}
}
// 判断学生信息是否匹配
private boolean isStudentMatched(String id, String username, String password) {
for (Student student : students) {
if (id.equals(student.getId()) && username.equals(student.getUserName()) && password.equals(String.valueOf(student.getPassword()))) {
return true;
}
}
return false;
}
```