有jsp学生信息管理系统的代码吗
时间: 2024-03-24 09:38:59 浏览: 63
学生信息管理系统JSP源代码
4星 · 用户满意度95%
这里提供一个简单的 JSP 学生信息管理系统的示例代码,仅供参考:
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="list.jsp">学生列表</a></li>
<li><a href="add.jsp">添加学生</a></li>
</ul>
</body>
</html>
```
list.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
<h2>学生列表</h2>
<table border="1">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<%
List<Map<String, Object>> students = (List<Map<String, Object>>) request.getAttribute("students");
if (students != null) {
for (Map<String, Object> student : students) {
%>
<tr>
<td><%=student.get("id")%></td>
<td><%=student.get("name")%></td>
<td><%=student.get("gender")%></td>
<td><%=student.get("age")%></td>
<td><a href="delete.jsp?id=<%=student.get("id")%>">删除</a></td>
</tr>
<%
}
}
%>
</tbody>
</table>
<p><a href="add.jsp">添加学生</a></p>
</body>
</html>
```
add.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>
<h2>添加学生</h2>
<form action="add.do" method="post">
<p>
<label>学号:<input type="text" name="id"></label>
</p>
<p>
<label>姓名:<input type="text" name="name"></label>
</p>
<p>
<label>性别:<input type="radio" name="gender" value="男">男<input
type="radio" name="gender" value="女">女</label>
</p>
<p>
<label>年龄:<input type="text" name="age"></label>
</p>
<p><input type="submit" value="提交"></p>
</form>
<p><a href="list.jsp">返回学生列表</a></p>
</body>
</html>
```
delete.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除学生</title>
</head>
<body>
<%
String id = request.getParameter("id");
if (id == null) {
response.sendRedirect("list.jsp");
} else {
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE id='" + id + "'";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
response.sendRedirect("list.jsp");
} else {
out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
</body>
</html>
```
add.do
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生</title>
</head>
<body>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
if (id == null || name == null || gender == null || age == null) {
out.println("参数错误");
} else {
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String sql = "INSERT INTO student(id, name, gender, age) VALUES('" + id + "', '" + name + "', '" + gender + "', '" + age + "')";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
response.sendRedirect("list.jsp");
} else {
out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
</body>
</html>
```
这个示例代码中,使用JSP页面来展示学生信息,使用JDBC连接MySQL数据库来完成学生信息的添加和删除操作。当然,这个示例代码并不完整,还有很多需要改进的地方,例如数据验证、分页查询等等。
阅读全文