javaweb写一个学生管理系统
时间: 2023-11-22 13:49:42 浏览: 105
以下是一个简单的JavaWeb学生信息管理系统的实现,包括学生信息的增删改查功能:
1. 首先创建一个名为Student的JavaBean类,用于存储学生信息,包括学号、姓名、性别、年龄等属性。
```java
public class Student {
private String id;
private String name;
private String gender;
private int age;
// 构造方法、getter和setter方法省略
}
```
2. 创建一个名为StudentDao的数据访问对象类,用于对学生信息进行增删改查操作。这里使用了JDBC连接数据库进行操作。
```java
public class StudentDao {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/student";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
// 添加学生信息
public void addStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 定义SQL语句
String sql = "INSERT INTO student(id, name, gender, age) VALUES(?, ?, ?, ?)";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getAge());
// 执行SQL语句
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 删除学生信息
public void deleteStudent(String id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 定义SQL语句
String sql = "DELETE FROM student WHERE id=?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setString(1, id);
// 执行SQL语句
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 修改学生信息
public void updateStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 定义SQL语句
String sql = "UPDATE student SET name=?, gender=?, age=? WHERE id=?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getId());
// 执行SQL语句
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 查询学生信息
public List<Student> queryStudent() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Student> list = new ArrayList<Student>();
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 定义SQL语句
String sql = "SELECT * FROM student";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 执行SQL语句
rs = pstmt.executeQuery();
// 处理结果集
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
list.add(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
```
3. 创建一个名为StudentServlet的Servlet类,用于处理学生信息管理系统的请求。这里使用了JSP页面进行展示。
```java
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置请求编码和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取操作类型
String type = request.getParameter("type");
// 根据操作类型进行相应的操作
if ("add".equals(type)) {
addStudent(request, response);
} else if ("delete".equals(type)) {
deleteStudent(request, response);
} else if ("update".equals(type)) {
updateStudent(request, response);
} else if ("query".equals(type)) {
queryStudent(request, response);
}
}
// 添加学生信息
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");
int age = Integer.parseInt(request.getParameter("age"));
Student student = new Student(id, name, gender, age);
// 调用数据访问对象添加学生信息
StudentDao dao = new StudentDao();
dao.addStudent(student);
// 跳转到查询学生信息页面
response.sendRedirect("query.jsp");
}
// 删除学生信息
private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取学生学号
String id = request.getParameter("id");
// 调用数据访问对象删除学生信息
StudentDao dao = new StudentDao();
dao.deleteStudent(id);
// 跳转到查询学生信息页面
response.sendRedirect("query.jsp");
}
// 修改学生信息
private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取学生信息
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
Student student = new Student(id, name, gender, age);
// 调用数据访问对象修改学生信息
StudentDao dao = new StudentDao();
dao.updateStudent(student);
// 跳转到查询学生信息页面
response.sendRedirect("query.jsp");
}
// 查询学生信息
private void queryStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 调用数据访问对象查询学生信息
StudentDao dao = new StudentDao();
List<Student> list = dao.queryStudent();
// 将查询结果存储到request对象中
request.setAttribute("list", list);
// 转发到查询学生信息页面
request.getRequestDispatcher("query.jsp").forward(request, response);
}
}
```
4. 创建一个名为add.jsp的JSP页面,用于添加学生信息。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
</head>
<body>
<h1>添加学生信息</h1>
<form action="StudentServlet" method="post">
<input type="hidden" name="type" value="add">
学号:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
性别:<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
5. 创建一个名为query.jsp的JSP页面,用于查询学生信息。
```html
<!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>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>
<a href="update.jsp?id=${student.id}&name=${student.name}&gender=${student.gender}&age=${student.age}">修改</a>
<a href="StudentServlet?type=delete&id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="add.jsp">添加学生信息</a>
</body>
</html>
```
6. 创建一个名为update.jsp的JSP页面,用于修改学生信息。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改学生信息</title>
</head>
<body>
<h1>修改学生信息</h1>
<form action="StudentServlet" method="post">
<input type="hidden" name="type" value="update">
学号:<input type="text" name="id" value="${param.id}" readonly><br>
姓名:<input type="text" name="name" value="${param.name}"><br>
性别:<input type="radio" name="gender" value="男" <c:if test="${param.gender=='男'}">checked</c:if>>男
<input type="radio" name="gender" value="女" <c:if test="${param.gender=='女'}">checked</c:if>>女<br>
年龄:<input type="text" name="age" value="${param.age}"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
```
以上就是一个简单的JavaWeb学生信息管理系统的实现。可以根据实际需求进行扩展和优化。
阅读全文