Servlet连接数据库实现用户登录的实现示例

2022-11-13 10:11:32 示例 用户登录 连接数据库

做一个小案例顺便复习一下jdbc的知识

一、需求:

用户在浏览器输入用户名和密码,如果数据库中有数据,提醒用户登录成功,如果没有数据,提醒用户重新登录

二、先复习一下JDBC

今天用的时候有些细节忘了,知识还是需要多次复习啊,并不是说学过就是自己的。

1.概述:

用java程序操作数据库的一个技术,是java程序连接数据库的一套标准,本质上就是一堆api

2.开发步骤:

2.1.导jar包:

对于java项目,直接将jar包复制到项目然后解析jar(add as library)就可以用了;对于WEB项目,需要将jar包放到Tomcat的lib目c录下,在web项目中,当Class.forName(“com.Mysql.jdbc.Driver”);时idea是不会去查找字符串,不会去查找驱动的。所以只需要把mysql-connector-java-5.1.7-bin.jar拷贝到tomcat下lib目录就可以了。今天这里出了问题,找到这种解决方案。

2.2.步骤:具体步骤都在代码里

注册驱动—>

Class.forName(“com.mysql.jdbc.Driver”);

获取连接—>

String url = “协议://IP地址:端口号/数据库的名字/”;
String url = “jdbc:mysql://localhost:3306/person”;
Connection c = DriverManager.getConnection(url, “root”, “root”);

写sql语句—>

String sql = "select * from user where name = and pwd = ";

获取传输器—>

PreparedStatement preparedStatement = connection.prepareStatement(sql);

设置值—>

preparedStatement.setObject(1,username);
preparedStatement.setObject(2,pwd);

获取结果集—>

ResultSet resultSet = preparedStatement.executeQuery();

解析结果集—>

resultSet.next()

三、代码实现:

1.登录界面代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录界面</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
 <fORM action="userLogin" method="post" style="text-align: center" >
     用户名:<input type="text" name="username"><br/>
     <br/>
     密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="passWord" name="pwd"><br/>
     <br>
     <input type="submit" value="提交">
 </form>
</body>
</html>

2.登录成功界面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>成功登录界面</title>
</head>
<body>
<h1> 恭喜登录成功!!!</h1>
</body>
</html>

3.servlet代码:

package cn.tedu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.Http.httpservlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
//配置访问路径
@WebServlet(urlPatterns = "/userLogin")
public class ServletLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决中文乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset = utf-8");
        //2.获取用户输入的名字和密码
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //3.连接数据库
        try {
            //3.1注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3.2获取连接
            String url = "jdbc:mysql://localhost:3306/person";
            connection = DriverManager.getConnection(url,"root","root");
            //3.3写sql
            String sql = "select * from user where name = ? and pwd = ?";
            //3.4获取传输器
            preparedStatement = connection.prepareStatement(sql);
            //3.5设置值
            preparedStatement.setObject(1,username);
            preparedStatement.setObject(2,pwd);
            //3.6返回结果集
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                //重定位,如果结果返回true,"跳转"到success.html
                response.sendRedirect("success.html");
            }else{
                String urls = "login.html";
                response.getWriter().write("用户不存在"+""+"<a href = '"+urls+"'>点击重新登录</a>");
            }

        } catch (Exception e) {
            e.printStackTrace();
            //3.6关闭资源
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

4.数据库数据

5.登录成功页面:

6.登录失败界面:

7.登录界面:

相关文章