首先准备数据库数据和登录页面
创建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', '1111@');INSERT INTO `user` VALUES ('2', 'lisi', '123321', '2222@');INSERT INTO `user` VALUES ('3', 'wangwu', '111222', '3333@');
登录页面index.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>login</title></head><body><h1>欢迎登陆</h1><form action="login" method="post">用户名<input type="text" placeholder="请输入用户名" name="user"><br />密码<input type="password" placeholder="请输入密码" name="pwd"> <inputtype="submit" value="登陆"></form></body></html>
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
点击登录
输入
zhangsan
1234
GitHub地址