jsp做一个简易增删改查界面
时间: 2023-10-06 12:05:27 浏览: 92
好的,以下是一个简单的JSP增删改查界面的示例代码:
1. 创建一个名为“index.jsp”的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>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<%
// 连接数据库,查询用户列表
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
String sql = "SELECT * FROM user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getInt("age") %></td>
<td><a href="edit.jsp?id=<%= rs.getInt("id") %>">编辑</a> | <a href="delete.jsp?id=<%= rs.getInt("id") %>"
onclick="return confirm('确定要删除吗?')">删除</a></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</table>
<br>
<a href="add.jsp">添加用户</a>
</body>
</html>
```
2. 创建一个名为“add.jsp”的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>
<form action="save.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="保存"></td>
</tr>
</table>
</form>
<br>
<a href="index.jsp">返回列表</a>
</body>
</html>
```
3. 创建一个名为“save.jsp”的JSP文件,代码如下:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 获取表单参数
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
// 连接数据库,保存用户信息
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
String sql = "INSERT INTO user(name, age) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
<script>
alert("保存成功");
window.location.href = "index.jsp";
</script>
```
4. 创建一个名为“edit.jsp”的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>
<form action="update.jsp" method="post">
<table>
<tr>
<td>ID:</td>
<td><%= request.getParameter("id") %><input type="hidden" name="id" value="<%= request.getParameter("id") %>"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" value="<%= request.getParameter("name") %>"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" value="<%= request.getParameter("age") %>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="保存"></td>
</tr>
</table>
</form>
<br>
<a href="index.jsp">返回列表</a>
</body>
</html>
```
5. 创建一个名为“update.jsp”的JSP文件,代码如下:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 获取表单参数
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
// 连接数据库,更新用户信息
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
String sql = "UPDATE user SET name = ?, age = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setInt(3, id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
<script>
alert("保存成功");
window.location.href = "index.jsp";
</script>
```
6. 创建一个名为“delete.jsp”的JSP文件,代码如下:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 获取表单参数
int id = Integer.parseInt(request.getParameter("id"));
// 连接数据库,删除用户信息
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
String sql = "DELETE FROM user WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
<script>
alert("删除成功");
window.location.href = "index.jsp";
</script>
```
以上就是一个简单的JSP增删改查界面的示例代码,你可以根据自己的需求进行修改和完善。需要注意的是,该示例代码并没有做任何安全验证,实际开发中需要对用户输入进行合法性检查和防止SQL注入等安全问题的处理。
阅读全文