jsp实现学生数据库管理系统代码
时间: 2024-06-08 12:07:03 浏览: 137
抱歉,我是一个自然语言处理的AI模型,无法直接提供代码。但我可以给你一些指导,帮助你开始实现这个项目。
首先,你需要确定你的系统有哪些功能,比如添加学生信息、删除学生信息、修改学生信息、查询学生信息等等。然后你需要设计数据库表,存储学生信息。主要包括以下几个字段:学生ID、姓名、性别、年龄、联系方式等。
接着,你需要编写JSP页面,实现对学生信息的增删改查功能,通过JDBC连接数据库,将数据保存到数据库中。
以下是一个简单的JSP实现学生数据库管理系统的示例代码:
index.jsp
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生数据库管理系统</title>
</head>
<body>
<h1>学生数据库管理系统</h1>
<a href="add.jsp">添加学生信息</a><br>
<a href="delete.jsp">删除学生信息</a><br>
<a href="update.jsp">修改学生信息</a><br>
<a href="query.jsp">查询学生信息</a><br>
</body>
</html>
```
add.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="addStudent.jsp" method="post">
<label>学生ID:</label>
<input type="text" name="id"/><br>
<label>姓名:</label>
<input type="text" name="name"/><br>
<label>性别:</label>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女<br>
<label>年龄:</label>
<input type="text" name="age"/><br>
<label>联系方式:</label>
<input type="text" name="contact"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
```
addStudent.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");
String contact = request.getParameter("contact");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
String sql = "insert into student values('"+id+"','"+name+"','"+gender+"','"+age+"','"+contact+"')";
stmt.executeUpdate(sql);
out.print("添加成功!");
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("添加失败!");
}
%>
</body>
</html>
```
delete.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="deleteStudent.jsp" method="post">
<label>学生ID:</label>
<input type="text" name="id"/><br>
<input type="submit" value="删除"/>
</form>
</body>
</html>
```
deleteStudent.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");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
String sql = "delete from student where id='"+id+"'";
stmt.executeUpdate(sql);
out.print("删除成功!");
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("删除失败!");
}
%>
</body>
</html>
```
update.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="updateStudent.jsp" method="post">
<label>学生ID:</label>
<input type="text" name="id"/><br>
<label>姓名:</label>
<input type="text" name="name"/><br>
<label>性别:</label>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女<br>
<label>年龄:</label>
<input type="text" name="age"/><br>
<label>联系方式:</label>
<input type="text" name="contact"/><br>
<input type="submit" value="修改"/>
</form>
</body>
</html>
```
updateStudent.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");
String contact = request.getParameter("contact");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
String sql = "update student set name='"+name+"',gender='"+gender+"',age='"+age+"',contact='"+contact+"' where id='"+id+"'";
stmt.executeUpdate(sql);
out.print("修改成功!");
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("修改失败!");
}
%>
</body>
</html>
```
query.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="queryStudent.jsp" method="post">
<label>学生ID:</label>
<input type="text" name="id"/><br>
<input type="submit" value="查询"/>
</form>
</body>
</html>
```
queryStudent.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");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Statement stmt = conn.createStatement();
String sql = "select * from student where id='"+id+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
out.print("学生ID:"+rs.getString("id")+"<br>");
out.print("姓名:"+rs.getString("name")+"<br>");
out.print("性别:"+rs.getString("gender")+"<br>");
out.print("年龄:"+rs.getString("age")+"<br>");
out.print("联系方式:"+rs.getString("contact")+"<br>");
} else {
out.print("未查询到学生信息!");
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("查询失败!");
}
%>
</body>
</html>
```
注意:以上示例代码仅供参考,还需要根据自己的具体需求进行修改和完善。同时,为了保证数据库安全,建议使用PreparedStatement代替Statement执行SQL语句。
阅读全文