建造一个注册页面
用户输入:
用户名
暗码
年数
注册乐成:——>跳转至登录页面举办登录
注册失败:——>文字或其他形式的提示皆可
2、浅易查询:
建造一个查询页面
输入用户名
显示该用户的用户名、暗码、年数
演示
1.启动进入登岸页面
2.点击注册,进入注册页面,乐成跳转到登录页面
失败则提示
回到登录页面,登录乐成进入查询页面
登录失败显示提示信息
输入用户名->显示该用户的用户名、暗码、年数
代码
dao
public class UserDao { private Connection conn = null; private PreparedStatement ps=null; private int result=0; private ResultSet rs=null; //用户注册 public int register(User user){ String sql="insert into users(name,password,age) value (?,?,?)"; try { //获取数据库毗连工具 conn= JDBCUtil.getConnection(); //获取数据库操纵工具 ps=conn.prepareStatement(sql); ps.setString(1,user.getName()); ps.setString(2,user.getPassword()); ps.setInt(3,user.getAge()); //执行sql result=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally { JDBCUtil.close(null,ps,conn); } return result; } //登录验证用户信息 public int login(String userName,String password){ String sql ="select count(*) from users where name=? and password=?"; try { conn=JDBCUtil.getConnection(); ps=conn.prepareStatement(sql); ps.setString(1,userName); ps.setString(2,password); rs=ps.executeQuery(); while (rs.next()){ result=rs.getInt("count(*)"); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.close(rs,ps,conn); } return result; } //按照用户名 显示用户名、暗码、年数 public User findByName(String userName){ String sql="select name,password,age from users where name=?"; User user = null; try { conn=JDBCUtil.getConnection(); ps=conn.prepareStatement(sql); ps.setString(1,userName); rs=ps.executeQuery(); while (rs.next()){ String name = rs.getString("name"); String password = rs.getString("password"); int age = rs.getInt("age"); user = new User(name,password,age); } } catch (Exception e) { e.printStackTrace(); }finally { JDBCUtil.close(null,ps,conn); } return user; } }
entity 实体类
public class User { private int id; private String name; private String password; private int age; //set... //get... //constructor... }
service
public class UserServiceImpl implements UserService { UserDao userDao = new UserDao(); // 注册 @Override public int register(User user) { return userDao.register(user); } // 登岸 @Override public int login(String userName, String password) { return userDao.login(userName,password); } // 按照用户名查找信息 @Override public User findByName(String userName) { return userDao.findByName(userName); } }
servlet
// FindByNameServlet public class FindByNameServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); UserService userService = new UserServiceImpl(); User user = userService.findByName(name); //将查询功效放入request浸染域 request.setAttribute("userInfo",user); request.getRequestDispatcher("/jsp/index.jsp").forward(request,response); } } // LoginServlet public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1 获取 String userName = request.getParameter("userName"); String password = request.getParameter("password"); //2 service挪用dao对数据库操纵 UserService userService = new UserServiceImpl(); int result = userService.login(userName, password); //3 乐成跳转到查询页面,失败跳转到失败页面 if (result>0){ response.sendRedirect("/jsp/index.jsp"); }else{ response.sendRedirect("/login_error.html"); } } } // RegisterServlet public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserService userService = new UserServiceImpl(); User user = null; int result = 0; //1【挪用请求工具】读取【请求头】参数信息,获得用户注册信息 String userName, password, age; userName = request.getParameter("userName"); password = request.getParameter("password"); age = request.getParameter("age"); user = new User(userName, password, Integer.valueOf(age)); //2 挪用userService——>userDao // 先查询用户是否存在 User byName = userService.findByName(userName); if (byName!=null){ request.setAttribute("info","用户已存在!"); request.getRequestDispatcher("https://www.jb51.net/jsp/register.jsp").forward(request,response); } // 注册 result = userService.register(user); //3 配置编码名目,防备乱码 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //注册乐成:——>跳转至登录页面举办登录 //注册失败:——>注册页面提示:注册失败 if (result == 1) { response.sendRedirect("/login.html"); } else { request.setAttribute("info","注册失败!"); request.getRequestDispatcher("https://www.jb51.net/jsp/register.jsp").forward(request,response); } } }
JDBCUtil