package shujukv.nn; /* * java简单数据库验证登录及小功能 */ import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Test1 { static Connection con; static Scanner p1=new Scanner(System.in); static String url="jdbc:mysql://localhost:3306/userdemo2"; //相当于路径 static String username="root"; //登录名 static String password="1998"; static PreparedStatement p; static ResultSet res; static String sql; static Scanner n=new Scanner(System.in); static int i=0; public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("数据库正在连接"); }catch(Exception e) { e.printStackTrace(); } try { // for(int i=0;i<6;i++) // { // Thread.sleep(600); // System.out.print("# "); // } System.out.println(); con=DriverManager.getConnection(url, username, password); System.out.println("数据库连接成功"); } catch (Exception e) { e.printStackTrace(); } return con; } //main函数 public static void main(String[] args) throws Exception { getConnection(); int m=0; while(m!=5) { System.out.println("欢迎使用T.T租机!"); System.out.println("1.注册账户"); System.out.println("2.登录系统"); System.out.println("3.管理员登录"); System.out.println("4.找回密码"); System.out.println("5.退出"); System.out.println("请输入数字选择功能"); m=n.nextInt(); switch(m) { case 1:zhuce();break; case 2:denglu();break; case 3:guanliyuan();break; case 4:mhchaxun();break; case 5:break; } } } //登录 public static void denglu() throws SQLException { System.out.println("请输入你的账号:"); String a=n.next(); System.out.println("请输入你的密码:"); String b=n.next(); sql = "select username,password from user where username=? and password=?"; //预处理 p=con.prepareStatement(sql); //标记 p.setString(1, a); p.setString(2, b); res=p.executeQuery(); if(res.next()) { System.out.println("登陆成功"); } else { i++; if(i==3) { Break(i); } System.out.println("登录失败,请重新登录!"); denglu(); } } public static void guanliyuan() throws SQLException { System.out.println("请输入你的admin账号:"); String a=n.next(); System.out.println("请输入你的admin密码:"); String b=n.next(); sql = "select username,password from useradmin where username=? and password=?"; //预处理 p=con.prepareStatement(sql); //标记 p.setString(1, a); p.setString(2, b); res=p.executeQuery(); if(res.next()) { System.out.println("登陆成功"); } else { i++; if(i==3) { Break(i); } System.out.println("登录失败,请重新登录!"); denglu(); } } //注册 public static void zhuce()throws SQLException, InterruptedException { System.out.println("请输入你的新账号:"); String a=n.next(); sql="select username from user where username=?"; //预处理验证 p=con.prepareStatement(sql); p=con.prepareStatement(sql); p.setString(1, a); res=p.executeQuery(); if(res.next()) { System.out.println("此账号已注册,请重新输入"); zhuce(); } else { int k=0; while(k==0){ System.out.println("请输入你的登录密码"); String b=n.next(); System.out.println("请再次输入你的确认密码"); String b22=n.next(); if(b.equals(b22)) { //判断两次输入的密码是否相同 System.out.println("请输入你找回密码的问题:"); String b3=n.next(); System.out.println("请输入问题的答案:"); String b33=n.next(); String sql2="insert into user(username,password,userwt,userda) values(?,?,?,?)"; java.sql.PreparedStatement res3=con.prepareStatement(sql2); res3.setString(1, a); res3.setString(2, b); res3.setString(3, b3); res3.setString(4, b33); res3.execute(); System.out.println("注册成功!"); k=1; System.out.println("你的账号为:"+a); System.out.println("你的密码为:"+b); } else { System.out.println("两次输入的密码不一致,请重新输入"); } } } } //找密码 //预设问题提示回答验证 //根据账号 模糊查询 public static void mhchaxun() throws Exception { System.out.println("请输入你的账号:"); String k=p1.next(); sql="select * from user where username=?"; p=con.prepareStatement(sql); p.setString(1, k); res=p.executeQuery(); if(res.next()) { System.out.println(res.getString(4)); Thread.sleep(500); System.out.println("请输入你的问题答案"); chazhao(k); } else { mhchaxun(); } } //根据问题查找 public static void chazhao(String kk) throws Exception { String k=p1.next(); p=con.prepareStatement("select * from user where userda=? and username=?"); p.setString(1, k); p.setString(2, kk); res=p.executeQuery(); if(res.next()) { System.out.println("核对正确,请输入新密码:"); String k1=n.next(); xgpassword(k1,kk); }else { ++i; if(i==3) { Break(i); } System.out.println("答案错误!,请重新输入"); chazhao(kk); } } //强行退出提醒 public static void Break(int a) { System.out.println("你已错误3次,已强行退出"); System.exit(0); } //修改密码 public static void xgpassword(String k1,String K2) throws Exception { sql="update user set password=? where username=?"; p=con.prepareStatement(sql); p.setString(1, k1); p.setString(2, K2); p.executeUpdate(); System.out.println("修改成功,即将返回登录界面————"); Thread.sleep(1000); } }