/** * Copyright (C), 2017-2017 * FileName: User * Author: ichimoku * Date: 2017/12/5 14:31 * version: 7.9.0 * <Author> <Date> <version> * 作者姓名 修改时间 版本号 */ public class User { //登陆查询 ----- 括号内接收从网页传来的用户输入的参数 public static Boolean CheckUser(String username,String password) { int count = 0; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } // 2.建立连接 try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "用户名", "密码"); /*System.out.println("建立连接成功!");*/ String sql = "SELECT id FROM account WHERE name=? AND password=?"; ps=conn.prepareStatement(sql); ps.setString(1,username); ps.setString(2,password); rs=ps.executeQuery();
while (rs.next()){ count++; } } catch (SQLException e) { System.out.println("建立连接失败"); } finally { // 3.关闭连接 try { if (null != conn) { conn.close(); } if (null != ps) { ps.close(); } /*System.out.println("关闭连接成功!");*/ } catch (SQLException e) { System.out.println("程序错误"); } } return count>0 ? true:false; } //新增用户 ----- 括号内接收从网页传来的用户输入的参数 public static void AddUser(String username,String password) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } // 2.建立连接 try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "用户名", "密码"); /*System.out.println("建立连接成功!");*/ String sql = "INSERT INTO account SET (name,password) VALUE (?,?)"; ps=conn.prepareStatement(sql); ps.setString(1,username); ps.setString(2,password); rs=ps.executeQuery(); } catch (SQLException e) { System.out.println("建立连接失败"); } finally { // 3.关闭连接 try { if (null != conn) { conn.close(); } if (null != ps) { ps.close(); } System.out.println("关闭连接成功!!!"); } catch (SQLException e) { System.out.println("程序错误"); } } } //修改密码 --- 括号内接收从网页传来的用户输入的参数 public static void UpdateUser(String newpassword,String username,String password) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } // 2.建立连接 try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "用户名", "密码"); /*System.out.println("建立连接成功!");*/ String sql = "UPDATE account SET password=? WHERE name=? AND password=?"; ps=conn.prepareStatement(sql); ps.setString(1,newpassword); ps.setString(2,username); ps.setString(3,password); rs=ps.executeQuery(); } catch (SQLException e) { System.out.println("建立连接失败"); } finally { // 3.关闭连接 try { if (null != conn) { conn.close(); } if (null != ps) { ps.close(); } System.out.println("关闭连接成功!"); } catch (SQLException e) { System.out.println("程序错误"); } } } }
/** * Copyright (C), 2017-2017 * FileName: Check * Author: ichimoku * Date: 2017/12/5 14:40 * version: 7.9.0 * <Author> <Date> <version> * 作者姓名 修改时间 版本号 */ @WebServlet(name="CheckUser",urlPatterns="/servlet/CheckUser") public class CheckUser extends HttpServlet{ private static final long serialVersionUID = 1L; public CheckUser(){ super(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从网页获取输入的用户名和密码 String username = new String(req.getParameter("username").getBytes("iso-8859-1"), "utf-8"); String password = req.getParameter("password"); //在控制台打印从网页获取到的用户名 System.out.println(username); //判断用户名和密码是否为空 if (username!=null && password!=null){ boolean flag= User.CheckUser(username,password); //通过flag返回值 为true就会执行if,false就会执行else if (flag){ req.getRequestDispatcher("/pages/user/login_success.html").forward(req, resp); }else{ //req.getRequestDispatcher("/pages/user//Welcome.jsp").forward(req, resp); } }else{ System.out.println("用户的输入为空"); } } }
/** * Copyright (C), 2017-2017 * FileName: Add * Author: ichimoku * Date: 2017/12/5 16:01 * version: 7.9.0 * <Author> <Date> <version> * 作者姓名 修改时间 版本号 */ @WebServlet(name="AddUser",urlPatterns="/servlet/AddUser") public class AddUser extends HttpServlet { private static final long serialVersionUID = 1L; public AddUser(){ super(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从网页获取输入的用户名和密码 String username = new String(req.getParameter("username").getBytes("iso-8859-1"), "utf-8"); String password = req.getParameter("password"); //在控制台打印从网页获取到的用户名 System.out.println(username); //判断用户名和密码是否为空 if (username!=null && password!=null){ boolean flag=User.CheckUser(username,password); //通过flag返回值 为true就会执行if,false就会执行else,因为我们要新增用户,如果返回ture就说明 //这个账号已经有人使用,所以我们要提示注册失败,如果返回false,说明数据库里没有这个账号,可以注册 //在else里运行AddUser的方法,成功数据库在数据库里新增注册数据! if (flag){ //req.getRequestDispatcher("/pages/user//Welcome.jsp").forward(req, resp); }else{ //req.getRequestDispatcher("/pages/user//Welcome.jsp").forward(req, resp); User.AddUser(username,password); req.getRequestDispatcher("/pages/user/login_success.html").forward(req, resp); } }else{ System.out.println("用户的输入为空"); } } }
/** * Copyright (C), 2017-2017 * FileName: Update * Author: ichimoku * Date: 2017/12/5 16:05 * version: 7.9.0 * <Author> <Date> <version> * 作者姓名 修改时间 版本号 */ @WebServlet(name="UpdateUser",urlPatterns="/servlet/UpdateUser") public class UpdateUser extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateUser(){ super(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从网页获取输入的用户名和密码 String username = new String(req.getParameter("username").getBytes("iso-8859-1"), "utf-8"); String password = req.getParameter("password"); String newpassword = req.getParameter("newpassword"); //在控制台打印从网页获取到的用户名 System.out.println(username); //判断用户名和密码是否为空 if (username!=null && password!=null){ boolean flag=User.CheckUser(username,password); //通过flag返回值 为true就会执行if,false就会执行else,因为我们要修改密码,所以当返回true,说明用户输入的账号密码 // 和数据库匹配成功!所以我们在if下面执行UpdateUser方法,修改数据库数值! if (flag){ User.UpdateUser(newpassword,username,password); req.getRequestDispatcher("/pages/user/login_success.html").forward(req, resp); }else{ //req.getRequestDispatcher("/pages/user//Welcome.jsp").forward(req, resp); } }else { System.out.println("用户的输入为空"); } } }