文章目录
结果演示
1. 添加
2.删除
3.修改
源代码:
实体类
package com.biienu.entity;
public class User {
private String id;
private String name;
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
}
jdbc工具类
package com.biienu.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtil {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException, IOException {
Properties p = new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
String url = p.getProperty("url");
String user = p.getProperty("user");
String password = p.getProperty("password");
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
}
index.jsp
<%@ page import="com.biienu.utils.JdbcUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.biienu.entity.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %><%--
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户信息</title>
</head>
<body>
<%
Connection connection = JdbcUtil.getConnection();
Statement statement = connection.createStatement();
String sql = "select id, name from t_user";
ResultSet resultSet = statement.executeQuery(sql);
List<User> list = new ArrayList<>();
while(resultSet.next()){
String id = resultSet.getString("id");
String name = resultSet.getString("name");
User u = new User(id, name);
list.add(u);
}
%>
<table>
<tr><th>用户id</th><th>用户姓名</th><th class="text-align" colspan="2">操作</th></tr>
<%
for(User u: list){
%>
<tr>
<td><%=u.getId()%></td>
<td><%=u.getName()%></td>
<td><a href="delete.jsp?id=<%=u.getId()%>">删除</a></td>
<td><a href="updateForm.jsp?id=<%=u.getId()%>&name=<%=u.getName()%>">修改</a></td>
</tr>
<%
}
%>
<tr><td class="text-align" colspan="4"><a href="addForm.jsp">添加用户</a></td></tr>
</table>
</body>
<script>
</script>
<style>
.text-align{
text-align: center
}
table{
width: auto;
height: 100px;
margin-left: 30%;
}
table,tr,td,th{
border: 1px solid gray;
}
</style>
</html>
add.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.biienu.utils.JdbcUtil" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<%
Connection connection = JdbcUtil.getConnection();
String sql = "insert into t_user values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
String userid = (String) request.getParameter("userid");
String username = (String) request.getParameter("username");
statement.setString(1, userid);
statement.setString(2, username);
int i = statement.executeUpdate();
if(i > 0){
response.sendRedirect("/myweb");
} else {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<h1>插入失败</h1>");
pw.flush();
}
%>
</body>
</html>
addForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加</title>
</head>
<body>
<form name="upadd" action="add.jsp" method="get">
<table>
<tr><td>用户姓名</td><td><input name="username" type="text" /></td></tr>
<tr><td colspan="2"><input type="submit" value="提交"></td></tr>
</table>
</form>
</body>
</html>
delete.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="com.biienu.utils.JdbcUtil" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Connection connection = JdbcUtil.getConnection();
String sql = "delete from t_user where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
String id = request.getParameter("id");
ps.setString(1,id);
int i = ps.executeUpdate();
if(i > 0){
response.sendRedirect("/myweb");
} else {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<h1>删除失败</h1>");
pw.flush();
}
%>
</body>
</html>
update.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="com.biienu.utils.JdbcUtil" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改</title>
</head>
<body>
<%
Connection connection = JdbcUtil.getConnection();
String sql = "update t_user set name=?";
PreparedStatement ps = connection.prepareStatement(sql);
String name = request.getParameter("username");
ps.setString(1,name);
int i = ps.executeUpdate();
if(i > 0){
response.sendRedirect("/myweb");
} else {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<h1>修改失败</h1>");
pw.flush();
}
%>
</body>
</html>
updateForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改</title>
</head>
<body>
<form action="update.jsp" method="get">
<table>
<tr><td>用户姓名</td><td><input name="username" type="text" value="<%=request.getParameter("name")%>"/></td></tr>
<tr><td colspan="2"><input type="submit" value="确认修改"></td></tr>
</table>
</form>
</body>
</html>
数据表
| t_user | CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
input type=“submit” value=“确认修改”>
数据表
| t_user | CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |