【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)); } }

    推荐阅读