利用MySql和IDEA,仅使用jsp和servlet建立学生信息管理系统,学生表包括学号和姓名,教师表包括教师号和姓名,课程表包括课程号和课程名,实现对学生表的增删改查和对教师表、课程表的查询,给出sql语句及代码实现
时间: 2024-05-15 14:14:24 浏览: 142
基于servlet和jsp实现的学生信息管理系统【内含数据库+附源码】
5星 · 资源好评率100%
首先,我们需要创建一个名为“student_management”的数据库,其中包含三个表:student(学生表)、teacher(教师表)和course(课程表)。
学生表(student):
```
CREATE TABLE student (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
number VARCHAR(20) NOT NULL
);
```
教师表(teacher):
```
CREATE TABLE teacher (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
number VARCHAR(20) NOT NULL
);
```
课程表(course):
```
CREATE TABLE course (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
number VARCHAR(20) NOT NULL
);
```
接下来,我们可以使用IDEA创建一个基于Maven的Web项目,并添加相应的依赖:
```xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
```
接下来,我们可以为项目添加一个servlet,处理学生表的增删改查操作。以下是servlet的代码:
```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 java.util.ArrayList;
import java.util.List;
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("/StudentServlet")
public class StudentServlet extends HttpServlet {
private Connection connection;
@Override
public void init() throws ServletException {
super.init();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student_management?useSSL=false&serverTimezone=UTC",
"root",
"password"
);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
add(request, response);
} else if ("delete".equals(action)) {
delete(request, response);
} else if ("update".equals(action)) {
update(request, response);
} else {
list(request, response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Student> students = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM student");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String number = resultSet.getString("number");
Student student = new Student(id, name, number);
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("students", students);
request.getRequestDispatcher("student_list.jsp").forward(request, response);
}
private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String number = request.getParameter("number");
try {
PreparedStatement preparedStatement = connection
.prepareStatement("INSERT INTO student(name, number) VALUES(?, ?)");
preparedStatement.setString(1, name);
preparedStatement.setString(2, number);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("StudentServlet");
}
private void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
try {
PreparedStatement preparedStatement = connection
.prepareStatement("DELETE FROM student WHERE id=?");
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("StudentServlet");
}
private void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String number = request.getParameter("number");
try {
PreparedStatement preparedStatement = connection
.prepareStatement("UPDATE student SET name=?, number=? WHERE id=?");
preparedStatement.setString(1, name);
preparedStatement.setString(2, number);
preparedStatement.setInt(3, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("StudentServlet");
}
}
```
以上代码包括了对学生表的增删改查操作,其中用到了JDBC驱动和servlet的生命周期方法。需要注意的是,在servlet中使用JDBC连接数据库时,需要在init()方法中初始化连接,并在destroy()方法中关闭连接。
最后,我们需要创建一个名为“student_list.jsp”的JSP页面,用于展示学生表中的数据。以下是JSP页面的代码:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Student List</title>
</head>
<body>
<h1>Student List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Number</th>
<th>Action</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.number}</td>
<td>
<a href="StudentServlet?action=delete&id=${student.id}">Delete</a>
<a href="student_edit.jsp?id=${student.id}&name=${student.name}&number=${student.number}">Edit</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="student_add.jsp">Add Student</a>
</body>
</html>
```
在该页面中,我们使用了JSTL标签库中的<c:forEach>标签,用于遍历学生列表并展示其信息。同时,在每个学生信息行中,我们添加了删除和编辑功能的超链接。
以上代码实现了学生信息管理系统的增删改查操作,可以通过类似的方式处理教师和课程表的查询操作。
阅读全文