Servlet+MySQL使用DBCP数据库连接池实现用户登录

首先准备数据库数据和登录页面
创建user数据表并且插入几条数据

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `password` varchar(16) DEFAULT NULL,
  `email` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', '[email protected]');
INSERT INTO `user` VALUES ('2', 'lisi', '123321', '[email protected]');
INSERT INTO `user` VALUES ('3', 'wangwu', '111222', '[email protected]');

登录页面index.html





login


    

欢迎登陆

用户名
密码

JCDB工具类
注意先导入必要的jar包
DBUtil.java

package com.wangc.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;

public class DBUtil {

    public static final String DRIVERNAME = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://localhost:3306/javadata";//注意数据库名
    public static final String USERNAME = "root";//使用自己的用户名和密码
    public static final String PASSWORD = "****";//使用自己的用户名和密码
    
    public static BasicDataSource bSource = null;

    static{
        bSource = new BasicDataSource();
        bSource.setDriverClassName(DRIVERNAME);
        bSource.setUrl(URL);
        bSource.setUsername(USERNAME);
        bSource.setPassword(PASSWORD);
    }
    
    public static Connection getConn(){
        try {
            return bSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void release(Connection conn, Statement stat, ResultSet rs){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

LoginServlet.java

package com.wangc.control;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.wangc.utils.DBUtil;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        try {
            conn = DBUtil.getConn();
            String sql = "select * from user where name=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, request.getParameter("user"));
            ps.setString(2, request.getParameter("pwd"));
            rs = ps.executeQuery();
            if (rs.next()) {
                response.getWriter().write("登陆成功!");
            }else {
                response.getWriter().write("登陆失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.release(conn, ps, rs);
        }
    }

}

测试一下
输入
zhangsan
123456
点击登录

Servlet+MySQL使用DBCP数据库连接池实现用户登录_第1张图片
image.png

Servlet+MySQL使用DBCP数据库连接池实现用户登录_第2张图片

输入
zhangsan
1234

Servlet+MySQL使用DBCP数据库连接池实现用户登录_第3张图片

GitHub地址

你可能感兴趣的:(Servlet+MySQL使用DBCP数据库连接池实现用户登录)