基于Servlet+JSP+Mysql的简易学生管理功能,实现增删改查代码
时间: 2023-12-14 09:02:21 浏览: 227
由于您没有提供具体的需求和数据结构,以下是一个基于Servlet+JSP+Mysql的简易学生管理功能的示例代码:
1. 创建数据库表
首先,我们需要在Mysql中创建一个学生表,可以使用以下SQL语句:
```
CREATE TABLE student (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age INT(11) NOT NULL,
gender VARCHAR(10) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 编写JavaBean
接下来,我们需要创建一个JavaBean,用于封装学生信息。可以使用以下代码:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
public Student() {
}
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
// getter and setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
3. 编写DAO类
接下来,我们需要创建一个DAO类,用于操作学生表。可以使用以下代码:
```java
public class StudentDAO {
private Connection conn;
public StudentDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&useSSL=false";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
String sql = "SELECT * FROM student";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
Student student = new Student(id, name, age, gender);
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public void addStudent(Student student) {
String sql = "INSERT INTO student(name, age, gender) VALUES (?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setString(3, student.getGender());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteStudent(int id) {
String sql = "DELETE FROM student WHERE id=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateStudent(Student student) {
String sql = "UPDATE student SET name=?, age=?, gender=? WHERE id=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setString(3, student.getGender());
ps.setInt(4, student.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Student getStudentById(int id) {
String sql = "SELECT * FROM student WHERE id=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
Student student = new Student(id, name, age, gender);
return student;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
```
4. 编写Servlet类
接下来,我们需要创建一个Servlet类,用于接收用户请求并调用DAO类来操作数据库。可以使用以下代码:
```java
public class StudentServlet extends HttpServlet {
private StudentDAO studentDAO;
public void init() {
studentDAO = new StudentDAO();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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 "delete":
deleteStudent(request, response);
break;
case "edit":
editStudent(request, response);
break;
case "update":
updateStudent(request, response);
break;
default:
listStudents(request, response);
break;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private void listStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> students = studentDAO.getAllStudents();
request.setAttribute("students", students);
RequestDispatcher dispatcher = request.getRequestDispatcher("/list.jsp");
dispatcher.forward(request, response);
}
private void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
Student student = new Student(0, name, age, gender);
studentDAO.addStudent(student);
response.sendRedirect("student?action=list");
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
studentDAO.deleteStudent(id);
response.sendRedirect("student?action=list");
}
private void editStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Student student = studentDAO.getStudentById(id);
request.setAttribute("student", student);
RequestDispatcher dispatcher = request.getRequestDispatcher("/edit.jsp");
dispatcher.forward(request, response);
}
private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
Student student = new Student(id, name, age, gender);
studentDAO.updateStudent(student);
response.sendRedirect("student?action=list");
}
}
```
5. 编写JSP页面
最后,我们需要创建两个JSP页面,用于显示学生列表和编辑学生信息。可以使用以下代码:
list.jsp:
```html
<%@ 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>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td>
<a href="student?action=edit&id=${student.id}">编辑</a>
<a href="student?action=delete&id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="add.jsp">添加学生</a>
</body>
</html>
```
edit.jsp:
```html
<%@ 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>
<form action="student?action=update" method="post">
<input type="hidden" name="id" value="${student.id}">
姓名:<input type="text" name="name" value="${student.name}"><br>
年龄:<input type="text" name="age" value="${student.age}"><br>
性别:<input type="text" name="gender" value="${student.gender}"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
```
6. 部署和运行
将以上代码保存在一个Java Web项目中,并部署到Tomcat服务器上。访问http://localhost:8080/项目名/student?action=list 即可浏览学生列表。
阅读全文