jsp+jdbc+mysql实现学生管理系统
时间: 2023-06-23 13:08:26 浏览: 119
学生管理系统是一个比较典型的CRUD应用,我们可以使用JSP + JDBC + MySQL来实现。
首先,我们需要创建一个数据库,包含学生信息的表。可以创建一个名为`student`的表,包含以下字段:
```
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
age int(11) NOT NULL,
gender varchar(10) NOT NULL,
phone varchar(20) DEFAULT NULL,
address varchar(100) DEFAULT NULL,
PRIMARY KEY (id)
```
接下来,我们需要创建一个JDBC连接池,以便在应用中共享数据库连接。可以使用Tomcat自带的连接池,或者使用第三方的连接池库。这里我们使用Tomcat自带的连接池。
在`context.xml`文件中添加以下内容:
```
<Context>
<Resource name="jdbc/student" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="数据库用户名" password="数据库密码" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8"/>
</Context>
```
在`web.xml`文件中添加以下内容:
```
<resource-ref>
<description>Connection pool for MySQL</description>
<res-ref-name>jdbc/student</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
```
然后,我们可以编写一个JavaBean来代表学生对象。在`Student.java`文件中添加以下内容:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String phone;
private String address;
// 省略getter和setter方法
}
```
接下来,我们可以编写一个DAO类来访问数据库。在`StudentDAO.java`文件中添加以下内容:
```java
public class StudentDAO {
private DataSource dataSource;
public StudentDAO() throws NamingException {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/student");
}
public List<Student> getAllStudents() throws SQLException {
List<Student> students = new ArrayList<>();
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student")) {
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setPhone(rs.getString("phone"));
student.setAddress(rs.getString("address"));
students.add(student);
}
}
return students;
}
public Student getStudentById(int id) throws SQLException {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student WHERE id=?")) {
stmt.setInt(1, id);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setPhone(rs.getString("phone"));
student.setAddress(rs.getString("address"));
return student;
}
}
}
return null;
}
public void addStudent(Student student) throws SQLException {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO student (name, age, gender, phone, address) VALUES (?, ?, ?, ?, ?)")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setString(3, student.getGender());
stmt.setString(4, student.getPhone());
stmt.setString(5, student.getAddress());
stmt.executeUpdate();
}
}
public void updateStudent(Student student) throws SQLException {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"UPDATE student SET name=?, age=?, gender=?, phone=?, address=? WHERE id=?")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setString(3, student.getGender());
stmt.setString(4, student.getPhone());
stmt.setString(5, student.getAddress());
stmt.setInt(6, student.getId());
stmt.executeUpdate();
}
}
public void deleteStudent(int id) throws SQLException {
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM student WHERE id=?")) {
stmt.setInt(1, id);
stmt.executeUpdate();
}
}
}
```
最后,我们可以编写一个JSP页面来展示学生信息。在`index.jsp`文件中添加以下内容:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="com.example.StudentDAO" %>
<%@ page import="com.example.Student" %>
<!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>
<th>地址</th>
<th></th>
</tr>
<%
StudentDAO dao = null;
try {
dao = new StudentDAO();
List<Student> students = dao.getAllStudents();
for (Student student : students) {
%>
<tr>
<td><%= student.getId() %></td>
<td><%= student.getName() %></td>
<td><%= student.getAge() %></td>
<td><%= student.getGender() %></td>
<td><%= student.getPhone() %></td>
<td><%= student.getAddress() %></td>
<td>
<a href="edit.jsp?id=<%= student.getId() %>">编辑</a>
<a href="delete.jsp?id=<%= student.getId() %>" onclick="return confirm('确定删除?')">删除</a>
</td>
</tr>
<%
}
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} finally {
if (dao != null) {
dao.close();
}
}
%>
</table>
<p><a href="add.jsp">添加学生</a></p>
</body>
</html>
```
在`edit.jsp`文件中,我们可以使用同样的方式显示学生信息,并提供一个表单来编辑学生信息。在提交表单时,我们可以使用`StudentDAO`类来更新学生信息。
在`add.jsp`文件中,我们可以提供一个表单来添加新的学生信息。在提交表单时,我们可以使用`StudentDAO`类来插入新的学生信息。
在`delete.jsp`文件中,我们可以使用`StudentDAO`类来删除学生信息。
至此,我们已经完成了一个简单的学生管理系统的实现。注意,在实际应用中,我们需要进行一些安全性和可靠性的考虑,例如输入验证、异常处理、事务管理等。
阅读全文