2.27开学测试代码

package Servlet;
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;

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的注册信息
String ad = request.getParameter("ad");
String psd = request.getParameter("psd");

// 将注册信息保存到数据库中
Connection conn = null;
PreparedStatement stmt = null;
try {
// 获取数据库连接
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tiaoji", "root", "123456");

// 构建 SQL 插入语句Userid
String sql = "INSERT INTO t1 (ad, psd ) VALUES (?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, ad);
stmt.setString(2, psd);

// 执行 SQL 插入语句
int rows = stmt.executeUpdate();
if (rows > 0) {
// 注册成功,跳转到登录页面
response.sendRedirect("Caiwurenyuan.jsp");
} else {
// 注册失败,返回注册页面并显示错误信息
response.sendRedirect("register.jsp?error=true");
}
} catch (SQLException e) {
// 处理数据库异常
e.printStackTrace();
response.sendRedirect("register.jsp?error=true");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
// 关闭数据库连接和相关资源
closeStatement(stmt);
closeConnection(conn);
}
}

private void closeConnection(Connection conn) {
// 关闭 Connection 对象
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

private void closeStatement(PreparedStatement stmt) {
// 关闭 PreparedStatement 对象
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package Servlet;
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;

@WebServlet("/RegisterServlet")

public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的注册信息
String Name = request.getParameter("Name");
String ID = request.getParameter("ID");
String Zhuanye = request.getParameter("Zhuanye");
String Totoalscore = request.getParameter("Totoalscore");
String ScoreA= request.getParameter("ScoreA");
String ScoreB = request.getParameter("ScoreB");
String ScoreC = request.getParameter("ScoreC");
String ScoreD = request.getParameter("ScoreD");
// 将注册信息保存到数据库中
Connection conn = null;
PreparedStatement stmt = null;
try {
// 获取数据库连接
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tiaoji", "root", "123456");

// 构建 SQL 插入语句Userid
String sql = "INSERT INTO t (Name, ID, Zhuanye, Totoalscore, ScoreA,ScoreB,ScoreC,ScoreD) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, Name);
stmt.setString(2, ID);
stmt.setString(3, Zhuanye);
stmt.setString(4, Totoalscore);
stmt.setString(5, ScoreA);
stmt.setString(6, ScoreB);
stmt.setString(7, ScoreC);
stmt.setString(8, ScoreD);
// 执行 SQL 插入语句
int rows = stmt.executeUpdate();
if (rows > 0) {
// 注册成功,跳转到登录页面
response.sendRedirect("Yanjiu.jsp");
} else {
// 注册失败,返回注册页面并显示错误信息
response.sendRedirect("register.jsp?error=true");
}
} catch (SQLException e) {
// 处理数据库异常
e.printStackTrace();
response.sendRedirect("register.jsp?error=true");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
// 关闭数据库连接和相关资源
closeStatement(stmt);
closeConnection(conn);
}
}

private void closeConnection(Connection conn) {
// 关闭 Connection 对象
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

private void closeStatement(PreparedStatement stmt) {
// 关闭 PreparedStatement 对象
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package Servlet;

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.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/update")
public class updateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int studentId = Integer.parseInt(request.getParameter("id"));
String newStudentName = request.getParameter("name");

// 获取数据库连接
Connection connection = null;
try {
connection = DatabaseUtil.getConnection();
if (connection != null) {
// 编写 SQL 查询语句
String sql = "UPDATE denglu SET ad=? WHERE psd=?";

// 使用 PreparedStatement 执行 SQL 查询语句
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, newStudentName);
preparedStatement.setInt(2, studentId);

// 执行更新操作
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
// 更新成功
response.getWriter().println("Student updated successfully!");
} else {
// 更新失败
response.getWriter().println("Failed to update student!");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
// 处理异常
} finally {
// 关闭连接
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2024/2/27
Time: 14:36
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body {
background-image: url('adc.jpg');
background-size: cover;
background-position: center;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}

.login-box {
width: 300px;
padding: 20px;
background: rgba(255, 255, 255, 0.8);
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 5px;
}

input[type="submit"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: none;
background: #007bff;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-box">
<h2>Login</h2>
<form action="LoginServlet" method="post">
<input type="text" name="ad" placeholder="ad" required>
<input type="password" name="psd" placeholder="psd" required>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>


<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2023/12/18
Time: 14:52
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>注册界面</title>
</head>
<body>
<form action="RegisterServlet" method="post">

姓名:<input type="text" name = "Name"><br>
报名号:<input type="text" name = "ID"><br>
调剂专业: <select name="Zhuanye">
<option value="text">计算机专业</option>
<option value="text">软件工程专业</option>
<option value="text">电子信息工程专业</option>
</select><br>
总分:<input type="text" name = "Totoalscore"><br>
思想政治理论:<input type="text" name = "ScoreA"><br>
英语:<input type="text" name = "ScoreB"><br>
数学:<input type="text" name = "ScoreC"><br>
专业综合:<input type="text" name = "ScoreD"><br>
<a href="jihua.jsp">浏览调剂计划</a>
</div>
<br>
<input type="submit" value="提交申请">

</form>
</body>
</html>




<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2024/2/27
Time: 16:11
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Display Form Data</title>
</head>
<body>
<h2>Form Data:</h2>
<p>Name: <%= request.getParameter("name") %></p>
<p>Registration Number: <%= request.getParameter("registration_number") %></p>
<p>Major: <%= request.getParameter("major") %></p>
<p>Total Score: <%= request.getParameter("total_score") %></p>
<p>Political Theory: <%= request.getParameter("political_theory") %></p>
<p>English: <%= request.getParameter("english") %></p>
<p>Math: <%= request.getParameter("math") %></p>
<p>Major Comprehensive: <%= request.getParameter("major_comprehensive") %></p>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2024/2/27
Time: 16:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>浏览调剂计划</title>
</head>
<body>
专业名称(限定在三个专业范围)
调剂人数
调剂总分要求
政治要求
英语要求
数学要求
截止时间
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 86156
Date: 2024/2/27
Time: 16:44
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>研究生工作业</h1>
<!--:发布调剂计划、审查学生资质、发布复试计划、发送录取通知、确认录取人员;-->
<a href="addtiaoji.html">发布调剂计划</a>
<a href="shencha.html">审查学生资质</a>
<a href="fabufushi.html">发布复试计划</a>
<a href="sendtongzhi.html">发送录取通知</a>
<a href="querenluqu.html">确认录取人员</a>
</body>
</html>
posted @ 2024-02-29 13:16  Code13  阅读(2)  评论(0编辑  收藏  举报