请帮我写一份javaee课程设计要求可以对数据库中的数据进行增删改查要求前端界面精美一些,请帮我写出jsp代码以及serverlet并且标注注释
时间: 2024-02-23 14:59:46 浏览: 106
(全家桶)基于JavaEE的毕业设计管理系统_JSP网站设计_SqlServer数据库设计(论文+源码+录像)
好的,以下是一份JavaEE课程设计要求:
## 需求说明
本项目要求实现一个基于JavaEE的学生信息管理系统。系统包括以下几个模块:
- 学生信息管理:实现对学生信息的增加、删除、修改、查询等操作。
- 班级信息管理:实现对班级信息的增加、删除、修改、查询等操作。
- 成绩信息管理:实现对学生成绩信息的增加、删除、修改、查询等操作。
## 技术要求
- 使用MySQL数据库存储数据。
- 使用JSP实现前端界面。
- 使用Servlet实现后端逻辑控制。
- 使用JDBC连接数据库。
## 功能要求
- 学生信息管理:学生信息包括学号、姓名、性别、年龄、班级等信息,实现对学生信息的增加、删除、修改、查询等操作。
- 班级信息管理:班级信息包括班级编号、班级名称、专业等信息,实现对班级信息的增加、删除、修改、查询等操作。
- 成绩信息管理:成绩信息包括学号、科目、成绩等信息,实现对学生成绩信息的增加、删除、修改、查询等操作。
- 页面设计:前端页面设计要求简洁美观,操作方便。
## 代码实现
以下是一份简单的JSP代码和Servlet代码实现:
### index.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<ul>
<li><a href="student.jsp">学生信息管理</a></li>
<li><a href="class.jsp">班级信息管理</a></li>
<li><a href="score.jsp">成绩信息管理</a></li>
</ul>
</body>
</html>
```
### StudentServlet.java
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
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;
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String DB_URL = "jdbc:mysql://localhost:3306/students";
private final String DB_USER = "root";
private final String DB_PASSWORD = "root";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "list":
listStudents(request, response);
break;
case "add":
addStudent(request, response);
break;
case "edit":
editStudent(request, response);
break;
case "delete":
deleteStudent(request, response);
break;
default:
listStudents(request, response);
break;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private void listStudents(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "SELECT * FROM student";
try (PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
request.setAttribute("students", rs);
request.getRequestDispatcher("studentList.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void addStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String clazz = request.getParameter("clazz");
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "INSERT INTO student (id, name, gender, age, clazz) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, gender);
ps.setString(4, age);
ps.setString(5, clazz);
ps.executeUpdate();
response.sendRedirect("student");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void editStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "SELECT * FROM student WHERE id=?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, id);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
request.setAttribute("student", rs);
request.getRequestDispatcher("studentEdit.jsp").forward(request, response);
} else {
response.sendRedirect("student");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String sql = "DELETE FROM student WHERE id=?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, id);
ps.executeUpdate();
response.sendRedirect("student");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
### studentList.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息列表</title>
</head>
<body>
<h1>学生信息列表</h1>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
<th colspan="2">操作</th>
</tr>
<c:forEach var="student" items="${requestScope.students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>${student.clazz}</td>
<td><a href="student?action=edit&id=${student.id}">修改</a></td>
<td><a href="student?action=delete&id=${student.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<br>
<a href="studentAdd.jsp">添加学生信息</a>
<br>
<a href="index.jsp">返回首页</a>
</body>
</html>
```
以上是简单的实现,具体的实现方式还需根据实际情况进行调整。
阅读全文