jsp增删改查页面代码
时间: 2023-12-25 11:04:49 浏览: 154
以下是一个简单的 JSP 页面的增删改查代码,以学生信息管理系统为例:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<h2>添加学生信息</h2>
<form method="post" action="addStudent.jsp">
姓名:<input type="text" name="name"><br>
学号:<input type="text" name="id"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="添加">
</form>
<h2>修改学生信息</h2>
<form method="post" action="updateStudent.jsp">
学号:<input type="text" name="id"><br>
修改后的姓名:<input type="text" name="name"><br>
修改后的年龄:<input type="text" name="age"><br>
<input type="submit" value="修改">
</form>
<h2>删除学生信息</h2>
<form method="post" action="deleteStudent.jsp">
学号:<input type="text" name="id"><br>
<input type="submit" value="删除">
</form>
<h2>查询学生信息</h2>
<form method="post" action="queryStudent.jsp">
学号:<input type="text" name="id"><br>
<input type="submit" value="查询">
</form>
</body>
</html>
```
其中,每个表单都有一个 `action` 属性,用于指定提交表单后的请求处理页面,这里分别是 `addStudent.jsp`、`updateStudent.jsp`、`deleteStudent.jsp` 和 `queryStudent.jsp`。
在每个请求处理页面中,可以通过 `request` 对象获取表单提交的数据,然后进行相应的操作,如添加、修改、删除或查询数据库中的学生信息。具体代码如下:
添加学生信息 `addStudent.jsp`:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
String name = request.getParameter("name");
String id = request.getParameter("id");
int age = Integer.parseInt(request.getParameter("age"));
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into student(name, id, age) values (?, ?, ?)";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, id);
ps.setInt(3, age);
ps.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
修改学生信息 `updateStudent.jsp`:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
Connection conn = null;
PreparedStatement ps = null;
String sql = "update student set name=?, age=? where id=?";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, id);
ps.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
删除学生信息 `deleteStudent.jsp`:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from student where id=?";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.executeUpdate();
response.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
查询学生信息 `queryStudent.jsp`:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from student where id=?";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
ps = conn.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
out.println("<h3>查询结果:</h3>");
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
out.println("姓名:" + name + "<br>");
out.println("学号:" + id + "<br>");
out.println("年龄:" + age + "<br>");
} else {
out.println("未找到该学生!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
以上代码仅作参考,实际应用需要根据具体需求进行修改和完善。
阅读全文