【Java】【JDBC】简单登陆
模拟简单登陆
/**
* 模拟登陆
*/
public class Demo {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String signinName = scanner.nextLine();
System.out.println("请输入密码:");
String pwd = scanner.nextLine();
User user = new User(signinName, pwd);
System.out.println(new User(signinName, pwd).login() ? "登陆成功" : "账号或密码不匹配");
}
}
/**
* 用户表实体类
*/
public class User {
private String signinName;
private String pwd;
public User(String signinName, String pwd) {
this.signinName = signinName;
this.pwd = pwd;
}@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (signinName != null ? !signinName.equals(user.signinName) : user.signinName != null) return false;
return pwd != null ? pwd.equals(user.pwd) : user.pwd == null;
}@Override
public int hashCode() {
int result = signinName != null ? signinName.hashCode() : 0;
result = 31 * result + (pwd != null ? pwd.hashCode() : 0);
return result;
}@Override
public String toString() {
return "User{" +
"signinName='" + signinName + '\'' +
", pwd='" + pwd + '\'' +
'}';
}public String getSigninName() {
return signinName;
}public void setSigninName(String signinName) {
this.signinName = signinName;
}public String getPwd() {
return pwd;
}public void setPwd(String pwd) {
this.pwd = pwd;
}/**
* 执行登陆
*
* @return boolean
* @throws Exception ?
*/
public boolean login() throws Exception {
Connection connection = MyDatabase.getConn();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM user WHERE signin_name=" + signinName);
String pwd = "";
while (resultSet.next()) {
pwd = resultSet.getString("pwd");
}
MyDatabase.kill(connection, statement, resultSet);
return pwd.equals(MyString.Md5(this.pwd));
}
}
推荐阅读
- 《别让小情绪害了你》D9
- Spring|Spring Data JPA
- 【成长-AI输出】20171024_数据与人工智能
- 【晨读感悟】三个月能流利说英语吗()
- 以前我心中的大学
- 喵喵机Max(这个小工具,能给学生党们带来很多便利)
- 咖啡冥想
- 【奇幻】《神奇龙与呆萌马的奇妙历险》(2)
- 我辜负了自己
- 过年