首页 > 其他分享 >2.27开学测试代码

2.27开学测试代码

时间:2024-02-29 13:25:06浏览次数:22  
标签:开学 request stmt javax sql 测试代码 import servlet 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>

标签:开学,request,stmt,javax,sql,测试代码,import,servlet,2.27
From: https://www.cnblogs.com/lijianlongCode13/p/18043455

相关文章

  • 2.27
    阅读笔记:《构建之法》《构建之法》是一本关于软件架构的书籍,作者是陈皓。本书主要介绍了软件系统的构建原则和方法,帮助读者理解如何设计可扩展、可维护和高性能的软件架构。主要内容:1.架构的重要性:作者强调了良好的架构对于软件系统的成功至关重要。一个好的架构能够提高开发效......
  • 2024.2.27模拟赛T2题解
    题目有一个神奇的结论\(\foralla<b<c,a\oplusc>min(a\oplusb,b\oplusc)\)然后就可以写出\(n^2\)dp,再用TRIE树优化即可code#include<bits/stdc++.h>usingnamespacestd;#defineN200005#defineintlonglongintn,k1,k2;inta[N],fl[2];constintm......
  • 2.27
    文化课考的最烂的一次。直升应该是选不上了,省选也打不了体验名额,所以我初三下学期直接奥赛摆了,一周来一两次就行了,还是中考更重要一些。来的时候遇到了喵喵,向他问了一些问题,他说我想停课也没问题,大概是指像直升的人一样的安排?但是我有那个资本吗?我文化课也不是很强足以支持我在......
  • 2.27 闲话 & solution 『你是太阳神倾倒而下美酒的甜香/是最高的永恒破碎之后的希望』
    考完试了,我想听歌写了几道LCT,但是都是板子我想听歌Cave洞穴勘测LCT板子啊,直接乱搞就行对于Connect操作和Destroy操作其实是Link和Cut的板子至于Query操作么...阿拉阿拉,直接对(u,v)都进行一次Find,然后判断是否相等即可核心代码就那么几行intn,m;FastI>>n>>m;while(m--......
  • 2.27
    今天上课进行了计算机网络和软件工程的课程.在上午的计算机网络的课程中我们学习了关于网络的历史和一些基本知识.像互联网之父,万维网,还有就是观看了一个关于万维网之父的在伦敦奥运会开幕式上的视频.知道了中国接入互联网的时间.1994年.在那之后一些互联网企业开始出现,像小米......
  • 大二下打卡(2.27)
    今天做了什么:上午,早上醒来,今天是英语四级考试出分的日子,先看了眼朋友圈跟QQ空间,很好,大家都查出来了,说明网站稳定,于是我立马打开浏览器查询我的分数,过了,得劲,多亏了高中三年的老底,下一次就是六月份的六级接着就是第一节计算机网络课,大二上学期的时候,建民老师就说,不知道什么原因,软......
  • 2.27每日总结
    今天课上学习了软件测试技术,对软件测试技术有了简单的了解。软件测试技术是一门涉及软件质量保证和验证的课程,旨在教授学生如何设计、执行和评估软件测试方案以确保软件系统的质量。这门课程通常涵盖以下内容:软件测试基础知识:介绍软件测试的基本概念、原则和方法论,包括白盒测......
  • Java测试代码编写
    一、单元测试1.1引入依赖1、root<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><s......
  • UI自动化测试代码不想写脚本不想配?RunnerGo一键录制
    想快速配置可视化UI自动化测试脚本?RunnerGo近期上线脚本录制器,根据你的测试操作直接生成UI自动化测试脚本,下面是使用方法Step1:下载录制器点击RunnerGo上方插件按钮下载录制器Step2:录制器使用将插件文件拖入浏览器扩展程序点击打开录制器,在浏览器中进行操作时录制器会将操作录制为......
  • 快速使用 timeit 测试代码运行速度
    Python中要测试代码速度,比起start_time=time.time()然后end_time=time.time(),更便捷的方法是使用timeit。以下是测试lambda表达式与普通判断语句速度的代码。运行10000次,打印各自运行时间。结果是lambda虽然优雅但更慢。importtimeitimportnumpyasnpensur......