Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -0,0 +1,67 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||
<%@ page import="java.sql.*" %>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登录</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>用户登录</h2>
|
||||
<form method="post" action="login.jsp">
|
||||
<div>
|
||||
用户名:<input type="text" name="name" required>
|
||||
</div>
|
||||
<div>
|
||||
密码:<input type="password" name="password" required>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit">登录</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<%
|
||||
if ("POST".equalsIgnoreCase(request.getMethod())) {
|
||||
String name = request.getParameter("name");
|
||||
String password = request.getParameter("password");
|
||||
|
||||
String url = "jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
|
||||
String dbUser = "root";
|
||||
String dbPwd = "1234";
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
conn = DriverManager.getConnection(url, dbUser, dbPwd);
|
||||
|
||||
String sql = "SELECT * FROM tb_user WHERE name = ? AND password = ?";
|
||||
pstmt = conn.prepareStatement(sql);
|
||||
pstmt.setString(1, name);
|
||||
pstmt.setString(2, password);
|
||||
rs = pstmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
out.println("<p>用户存在</p >");
|
||||
} else {
|
||||
out.println("<p>用户不存在</p >");
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
out.println("<p>数据库驱动加载失败!</p >");
|
||||
} catch (SQLException e) {
|
||||
out.println("<p>数据库操作失败:" + e.getMessage() + "</p >");
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null) rs.close();
|
||||
if (pstmt != null) pstmt.close();
|
||||
if (conn != null) conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
%>
|
||||
</body>
|
||||
</html>
|
||||
@ -0,0 +1,16 @@
|
||||
# JDBC 登录演示项目
|
||||
基于 JDBC 的用户登录验证 Demo
|
||||
|
||||
## 功能
|
||||
- 实现用户账号密码的数据库校验
|
||||
- 练习 JSP + Servlet + MySQL 开发流程
|
||||
|
||||
## 技术栈
|
||||
- Java
|
||||
- JSP/Servlet
|
||||
- MySQL 8.0
|
||||
- Tomcat 7
|
||||
|
||||
## 作者
|
||||
zhangsiyi
|
||||
2026-03-27
|
||||
Loading…
Reference in new issue