📄 userinfodao.java
字号:
package cn.edu.jlu.csw.team5.dbase;
import java.sql.*;
public class UserInfoDao {
private Connection con;
public UserInfoDao(Connection con) {
this.con = con;
}
public String validatePwd(String username, String password) {
PreparedStatement ps = null;
ResultSet rs = null;
String validated = "error.password.invalid";
String sql = "select * from userInfo where username=?";
try {
if (con.isClosed()) {
System.out.println("con is closed");
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next()) {
if (!rs.getString("password").trim().equals(password)) {
return validated; //口令不正确返回口令不匹配信息
} else {//检验用户的权限
if (rs.getString("id").trim().equals("user")) {
validated = "user"; //口令正确返回普通用户信息
return validated;
} else {
validated = "admin"; //口令正确返回管理员信息
return validated;
}
}
} else {
validated = "error.noMatch"; //没有找到该用户
return validated;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -