实体类
public class pah {
private String id;
private String name;
private String word;
private String phone;
public pah(String id, String name, String word, String phone) {
this.id = id;
this.name = name;
this.word = word;
this.phone = phone;
}
public pah() {
}
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 String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
链接MYSQL DBUtils
public class DBUtils {
//连接数据库--获取连接
public static Connection getConn(){
//MySQL8.0
// String driverName = "com.mysql.cj.jdbc.Driver";
// String url = "jdbc:mysql://localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//MySQL5.0
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
Connection conn = null;
try {
//1.加载(注册)JDBC驱动
Class.forName(driverName);
//2.获取链接
conn = DriverManager.getConnection(url, user, password);//连接对象(缆绳)
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//连接数据库--释放资源
public static void closeAll(ResultSet rs, Statement stmt, Connection conn){
try {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Dao包
public class Dao {
//添加
public static boolean add(pah pah) {
Connection conn =DBUtils.getConn();
PreparedStatement ptmt = null;
String sql = "INSERT INTO dept VALUES (?,?,?,?)";
boolean flag =false;
try {
ptmt = conn.prepareStatement(sql);
ptmt.setString(1, pah.getId());
ptmt.setString(2, pah.getName());
ptmt.setString(3, pah.getWord());
ptmt.setString(4, pah.getPhone());
int a = ptmt.executeUpdate();
if (a != 0){
flag = true;
}
DBUtils.closeAll(null, ptmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//列表显示
public static List<pah> list() {
String sql = "select * from dept";
List<pah> list = new ArrayList<>();
Connection conn = DBUtils.getConn();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
pah bean = null;
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String word= rs.getString("word");
String Phone = rs.getString("Phone");
bean = new pah(id,name,word,Phone);
list.add(bean);
/* bean=new pah();
bean.setId(rs.getString("id"));
bean.setName(rs.getString("name"));
bean.setWord(rs.getString("word"));
bean.setPhone(rs.getString("Phone"));
list.add(bean);*/
}
DBUtils.closeAll(rs, stmt, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//删除
public static void delete(String id) {
Connection conn= DBUtils.getConn();
String sql = "delete from dept where id =" + id ;//删除
PreparedStatement ptmt = null;
try {
ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行
int a = ptmt.executeUpdate();
DBUtils.closeAll(null,ptmt,conn);
}catch (Exception e) {
e.printStackTrace();
}
}
//修改
public static boolean update(pah pah,String id) {
Connection conn = DBUtils.getConn();
PreparedStatement ptmt = null;
String sql = " UPDATE dept SET id=?,name=?,word=?,Phone=? WHERE id="+id;
boolean flag =false;
try {
ptmt = conn.prepareStatement(sql);
ptmt.setString(1, pah.getId());
ptmt.setString(2, pah.getName());
ptmt.setString(3, pah.getWord());
ptmt.setString(4, pah.getPhone());
int a = ptmt.executeUpdate();
if (a != 0){
flag = true;
}
DBUtils.closeAll(null, ptmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
Servlet包
添加
@WebServlet("/addUser")
public class addServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
pah pah = new pah(id,username,password,phone);
if (Dao.add(pah)){
out.println("<h3 align=center>添加成功</h3>");
out.println("<h3 align=center><a href='userList.jsp'>查看用户列表</a></h3>");
}else {
out.println("添加失败");
}
/* try {
Connection conn = DBUtils.getConn();
Statement stmt = conn.createStatement();
String sql = "insert into dept values("+id+",'"+username+"','"+password+"','"+phone+"')";
int i = stmt.executeUpdate(sql);
if (i != 0){
out.println("<h3 align=center>添加成功</h3>");
out.println("<h3 align=center><a href='userList.html'>查看用户列表</a></h3>");
}else {
out.println("添加失败");
}
DBUtils.closeAll(null,stmt,conn);
} catch (SQLException e) {
e.printStackTrace();
}
out.close();
*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
删除
@WebServlet("/deleteUser")
public class deleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
Dao.delete(id);
response.sendRedirect("userList.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
修改
@WebServlet( "/toUpdate")
public class updateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
String phone = request.getParameter("phone");
ServletContext application = getServletContext();
String id1 = (String) application.getAttribute("id1");
System.out.println(id1);
pah pah = new pah(id,username,password,phone);
if (Dao.update(pah,id1)){
out.println("<h3 align=center>修改成功</h3>");
out.println("<h3 align=center><a href='userList.jsp'>查看用户列表</a></h3>");
}else {
out.println("添加失败");
out.println("<h3 align=center><a href='userList.jsp'>查看用户列表</a></h3>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
web 网页
显示主页
<%@ page import="service.pah" %>
<%@ page import="java.util.List" %>
<%@ page import="Dao.Dao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/common.css">
<script type="text/javascript">
function alert(id) {
var choose = window.confirm("确定删除吗?");
if (choose){
//执行删除
window.location="deleteUser?id="+id;
}
}
</script>
</head>
<body>
<div id="container">
<h1>用户信息列表</h1>
<a href="insert.html">添加用户</a>
<table>
<tr>
<td>用户编号</td>
<td>用户姓名</td>
<td>用户密码</td>
<td>联系方式</td>
<td>操作</td>
</tr>
<%
List<pah> list = Dao.list();
for (int i = 0; i <list.size() ; i++) {
String id = list.get(i).getId();
String name = list.get(i).getName();
String word = list.get(i).getWord();
String phone = list.get(i).getPhone();
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=word%></td>
<td><%=phone%></td>
<td><a href="toUpdate.jsp?id=<%=id%>">修改</a> <a href="javascript:alert(<%=id%>)">删除</a></td>
</tr>
<%
}
%>
</table>
</div>
</body>
</html>
添加页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>插入数据</title>
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<div id="container">
<h1>用户信息</h1>
<form action="addUser" method="post">
<table>
<tr>
<td>用户编号:</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>用户名称:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>联系方式:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加用户" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
修改页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改数据</title>
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<div id="container">
<%
String id1 = request.getParameter("id");
application.setAttribute("id1",id1);
%>
<h1><%=id1%>用户信息</h1>
<form action="toUpdate" method="post">
<table>
<tr>
<td>用户编号:</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>用户名称:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>联系方式:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="确定修改" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>