利用JDBC及Servlet实现对登录注册功能的实现;
1.题目要求:
1、新建一个数据库名为(个人姓名拼音),表(学生所在城市),字段(sid:学号,sname:姓名,sage:年龄,scontent:简介)
2、实现一个注册页面register.jsp,需包含以上信息的输入
3、利用Servlet实现对数据的接受及JDBC的保存
4、实现一个显示页面show.jsp。从数据库中返回以上所有数据(或EL表达式)
2.实施效果
3.实施代码
register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<title>信息录入</title>
</head>
<body>
<%--<h1>学生信息注册</h1>--%>
<form action="${pageContext.request.contextPath}/register" method="post">
<label>学号:</label>
<input type="text" name="sid"><br>
<label>姓名:</label>
<input type="text" name="sname"><br>
<label>年龄:</label>
<input type="text" name="sage"><br>
<label>简介:</label>
<input type="text" name="scontent"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
style.css
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
h1 {
float: top;
text-align: center;
}
form {
display: flex;
flex-direction: column;
align-items: center;
width: 300px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
input[type=text]{
width: 100%;
padding: 12px 20px;
margin: 8px 0;
border: 1px solid #ccc;
box-sizing: border-box;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
input[type=submit]:hover {
background-color: #45a049;
}
.error {
color: red;
}
.success {
color: green;
}
show.jsp
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: ALASIJIA
Date: 2023/11/16
Time: 11:29
To change this template use File | Settings | File Templates.
--%>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>信息展示</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #333;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>学生信息</h1>
<table>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>简介</th>
</tr>
<%
// 数据库连接信息
String url = "jdbc:mysql://localhost:3307/person_info?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String username = "root";
String password = "123456";
// 建立数据库连接
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
// 查询学生信息
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM stu_city");
// 遍历结果集并显示学生信息
while (rs.next()) {
String sid = rs.getString("sid");
String sname = rs.getString("sname");
String sage = rs.getString("sage");
String scontent = rs.getString("scontent");
out.println("<tr>");
out.println("<td>" + sid + "</td>");
out.println("<td>" + sname + "</td>");
out.println("<td>" + sage + "</td>");
out.println("<td>" + scontent + "</td>");
out.println("</tr>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接和资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
</table>
</body>
</html>
JDBCServlet
package com.hjj.servlet.hw10;
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.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.TimeZone;
/**
* @author:嘉佳 Date:2023/11/16 10:46
**/
@WebServlet("/register")
public class JDBCServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String sid = req.getParameter("sid");
String sname = req.getParameter("sname");
String sage = req.getParameter("sage");
String scontent = req.getParameter("scontent");
Connection connection=null;
PreparedStatement stmt=null;
String url="jdbc:mysql://localhost:3307/person_info?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC";
String user="root";
String password="123456";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO stu_city (sid, sname, sage, scontent) VALUES (?, ?, ?, ?)";
stmt=connection.prepareStatement(sql);
stmt.setInt(1, Integer.parseInt(sid));
stmt.setString(2,sname);
stmt.setInt(3, Integer.parseInt(sage));
stmt.setString(4,scontent);
// System.out.println(stmt);
stmt.executeUpdate();
// if (rs>0)
// resp.sendRedirect("show.jsp");
resp.sendRedirect("hw10/show.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}