【初学javeEE】如何通过使用数据库实现简单的用户登陆注册页面功能

  1. 登陆:
    jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆页面


用户名:
密码:

servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312");
		response.setCharacterEncoding("gb2312");
		request.setCharacterEncoding("UTF-8");
		// TODO Auto-generated method stub
		String user = request.getParameter("user");   //从表单获取用户名
		String pass = request.getParameter("password");   //从表单获取密码
		System.out.println(user+pass);	//测试
		//判断
		if (connectsql(user, pass) == 1){
			response.getWriter().println("登陆成功!"+"Hello " + user + "!");
			response.getWriter().println("
返回登陆页面"); } else if (connectsql(user, pass) == 2) { response.getWriter().println("密码错误!请重新输入"); response.getWriter().println("
返回登陆页面"); } else { response.getWriter().println("登陆失败!"+"Sorry " + user + "!"+"请先进行注册"); response.getWriter().println("
返回登陆页面"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } @SuppressWarnings("resource") protected int connectsql(String username, String password){ Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); // 加载数据库驱动 // 通过驱动管理类获取数据库链接 connection = DriverManager .getConnection("jdbc:mysql://localhost:3306/login?serverTimezone=UTC&characterEncoding=utf-8","root", "PdM46t"); String sql = "select * from record"; // 定义sql语句 // 获取预处理statement preparedStatement = connection.prepareStatement(sql); // 向数据库发出sql执行查询,查询出结果集 resultSet = preparedStatement.executeQuery(); // 遍历查询结果集 while (resultSet.next()) { System.out.println(resultSet.getString("user") + " " + resultSet.getString("password")); if (resultSet.getString("user").equals(username)) if (resultSet.getString("password").equals(password)) return 1; //用户名密码正确,返回1 else return 2; //用户名正确,密码错误,返回2 } } catch (Exception e) { e.printStackTrace(); } finally { // 释放资源 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //用户名不存在,返回0 return 0; }
  1. 注册:

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




注册页面


用户名:
密码:

servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312");
		response.setCharacterEncoding("gb2312");
		request.setCharacterEncoding("UTF-8");
		  String user = request.getParameter("user");   //从表单获取用户名
		String pass = request.getParameter("password");   //从表单获取密码
		//response.getWriter().println(user+pass);	//测试
			connectsql(user,pass);
		  response.getWriter().println("注册成功");
		  response.getWriter().println("
返回登陆页面"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } protected int connectsql(String username, String password){ Connection con = null; PreparedStatement pstm = null; try { // 加载数据库驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 通过驱动管理类获取数据库链接 con= DriverManager .getConnection( "jdbc:mysql://localhost:3306/login?serverTimezone=UTC&characterEncoding=utf-8", "root", "PdM46t"); String sql = "insert into record(user,password) value(?,?)"; pstm = con.prepareStatement(sql); pstm.setString(1, username); pstm.setString(2, password); int row = pstm.executeUpdate(); System.out.println("新增数据为:" + row + "条"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally{ if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pstm != null){ try { pstm.close(); } catch (SQLException e) { e.printStackTrace(); } } } return 0; }

数据库:
【初学javeEE】如何通过使用数据库实现简单的用户登陆注册页面功能_第1张图片

你可能感兴趣的:(JavaEE)