CRUD
由于重新安装ecplise,不小心将我之前写过的程序都弄丢了,现在重新写了一个增删改查简单版,效果如下;
数据库:
主页面:
增加:
删除:
修改:
查询:
目录如下:
代码如下:
Dao.java
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import database.Database; 10 import util.User; 11 12 public class Dao { 13 //增加 14 public boolean add(User user) { 15 Connection conn=null; 16 PreparedStatement pstm=null; 17 boolean judge=false; 18 try { 19 conn=Database.getConnection(); 20 String sql="insert into crud(name,password,sex,phone) values(?,?,?,?)"; 21 pstm=conn.prepareStatement(sql); 22 pstm.setString(1, user.getName()); 23 pstm.setString(2, user.getPassword()); 24 pstm.setString(3, user.getSex()); 25 pstm.setString(4,user.getPhone()); 26 //执行插入操作 27 int num=pstm.executeUpdate(); 28 if(num>0) { 29 System.out.println("插入成功"); 30 judge=true; 31 }else { 32 System.out.println("插入失败"); 33 judge=false; 34 } 35 }catch(Exception e) { 36 e.printStackTrace(); 37 }finally { 38 //SQL执行完成后释放相关资源 39 Database.close(conn,pstm); 40 } 41 return judge; 42 } 43 //查询 44 public List<User> find(){ 45 List<User> list=new ArrayList<>(); 46 Connection conn=null; 47 ResultSet rs=null; 48 PreparedStatement pstm=null; 49 User user=null; 50 try { 51 conn=Database.getConnection(); 52 String sql="select * from crud"; 53 pstm=conn.prepareStatement(sql); 54 rs=pstm.executeQuery(); 55 while(rs.next()) { 56 String name=rs.getString("name"); 57 String password=rs.getString("password"); 58 String sex=rs.getString("sex"); 59 String phone=rs.getString("phone"); 60 user=new User(name,password,sex,phone); 61 list.add(user); 62 63 System.out.println("name:"+name); 64 System.out.println("password:"+password); 65 System.out.println("sex:"+sex); 66 System.out.println("phone:"+phone); 67 } 68 }catch(Exception e) { 69 e.printStackTrace(); 70 }finally { 71 Database.close(conn, pstm, rs); 72 } 73 return list; 74 } 75 //删除 76 public boolean delete(String name) { 77 Connection conn=null; 78 PreparedStatement pstm=null; 79 ResultSet rs=null; 80 boolean judge=false; 81 try { 82 conn=Database.getConnection(); 83 String sql="delete from crud where name=?"; 84 pstm=conn.prepareStatement(sql); 85 pstm.setString(1, name); 86 int num=pstm.executeUpdate(); 87 if(num>0) { 88 System.out.println("删除成功"); 89 judge=true; 90 }else { 91 System.out.println("删除失败"); 92 judge=false; 93 } 94 }catch(Exception e) { 95 e.printStackTrace(); 96 }finally { 97 //SQL执行完成后释放相关资源 98 Database.close(conn,pstm,rs); 99 } 100 return judge; 101 } 102 //修改 103 public List<User> alter(String name){ 104 List<User> list=new ArrayList<>(); 105 Connection conn=null; 106 ResultSet rs=null; 107 PreparedStatement pstm=null; 108 User user=null; 109 try { 110 conn=Database.getConnection(); 111 String sql="select * from crud where name=?"; 112 pstm=conn.prepareStatement(sql); 113 pstm.setString(1,name); 114 rs=pstm.executeQuery(); 115 while(rs.next()) { 116 String namee=rs.getString("name"); 117 String password=rs.getString("password"); 118 String sex=rs.getString("sex"); 119 String phone=rs.getString("phone"); 120 user=new User(namee,password,sex,phone); 121 list.add(user); 122 123 System.out.println("name:"+namee); 124 System.out.println("password:"+password); 125 System.out.println("sex:"+sex); 126 System.out.println("phone:"+phone); 127 } 128 }catch(Exception e) { 129 e.printStackTrace(); 130 }finally { 131 Database.close(conn, pstm, rs); 132 } 133 return list; 134 } 135 //修改——更新 136 public boolean update(User user,String judgename) { 137 Connection conn=null; 138 PreparedStatement pstm=null; 139 boolean judge=false; 140 try { 141 conn=Database.getConnection(); 142 String sql="update crud set name=?,password=?,sex=?,phone=? where name=?"; 143 pstm=conn.prepareStatement(sql); 144 pstm.setString(1, user.getName()); 145 pstm.setString(2, user.getPassword()); 146 pstm.setString(3, user.getSex()); 147 pstm.setString(4,user.getPhone()); 148 pstm.setString(5, judgename); 149 //执行插入操作 150 int num=pstm.executeUpdate(); 151 if(num>0) { 152 System.out.println("修改成功"); 153 judge=true; 154 }else { 155 System.out.println("修改失败"); 156 judge=false; 157 } 158 }catch(Exception e) { 159 e.printStackTrace(); 160 }finally { 161 //SQL执行完成后释放相关资源 162 Database.close(conn,pstm); 163 } 164 return judge; 165 } 166 }
Database.java
1 package database; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class Database { 10 public static final String url="jdbc:mysql://localhost:3306/crud";//URL 11 public static final String user="填写自己的用户名";//用户名 12 public static final String password="填写自己的密码";//密码 13 14 /** 15 * 连接数据库 16 * @return 17 */ 18 public static Connection getConnection(){ 19 Connection conn=null; 20 try { 21 Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动 22 conn=DriverManager.getConnection(url, user, password); 23 System.out.println("数据库连接成功!"); 24 }catch(Exception e) { 25 e.printStackTrace(); 26 } 27 return conn; 28 } 29 30 /** 31 * 关闭数据库 32 */ 33 public static void close(Connection conn,PreparedStatement pstm) { 34 35 System.out.println("关闭SQL(conn,pstm)"); 36 if(pstm!=null) { 37 try { 38 pstm.close(); 39 }catch(SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 44 if(conn!=null) { 45 try { 46 conn.close(); 47 }catch(SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 52 } 53 54 public static void close(Connection conn,PreparedStatement pstm,ResultSet rs) { 55 56 System.out.println("关闭SQL(conn,pstm,rs)"); 57 if(pstm!=null) { 58 try { 59 pstm.close(); 60 }catch(SQLException e) { 61 e.printStackTrace(); 62 } 63 } 64 65 if(conn!=null) { 66 try { 67 conn.close(); 68 }catch(SQLException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 if(rs!=null) { 74 try { 75 rs.close(); 76 }catch(SQLException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 } 82 }
Servlet.java
1 package servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import dao.Dao; 13 import util.User; 14 15 /** 16 * Servlet implementation class Servlet 17 */ 18 @WebServlet("/Servlet") 19 public class Servlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public Servlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 Dao dao=new Dao(); 31 32 public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 response.setCharacterEncoding("UTF-8"); 34 request.setCharacterEncoding("UTF-8"); 35 36 String name=request.getParameter("name"); 37 String password=request.getParameter("password"); 38 String sex=request.getParameter("sex"); 39 String phone=request.getParameter("phone"); 40 41 System.out.println("name:"+name); 42 System.out.println("password:"+password); 43 System.out.println("sex:"+sex); 44 System.out.println("phone:"+phone); 45 46 User user=new User(name,password,sex,phone); 47 48 if(dao.add(user)){ 49 System.out.println("添加成功"); 50 request.getRequestDispatcher("main.jsp").forward(request,response); 51 }else { 52 System.out.println("添加失败"); 53 request.getRequestDispatcher("add.jsp").forward(request,response); 54 } 55 56 } 57 public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 58 response.setCharacterEncoding("UTF-8"); 59 request.setCharacterEncoding("UTF-8"); 60 61 List<User> list=dao.find(); 62 request.setAttribute("list", list); 63 64 System.out.println("查询成功"); 65 request.getRequestDispatcher("find.jsp").forward(request,response); 66 67 } 68 public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 69 response.setCharacterEncoding("UTF-8"); 70 request.setCharacterEncoding("UTF-8"); 71 72 List<User> list=dao.find(); 73 request.setAttribute("list", list); 74 request.getRequestDispatcher("delete.jsp").forward(request,response); 75 76 } 77 public void delete_show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 78 response.setCharacterEncoding("UTF-8"); 79 request.setCharacterEncoding("UTF-8"); 80 81 String name=request.getParameter("name"); 82 System.out.println("name:"+name); 83 84 if(dao.delete(name)){ 85 System.out.println("删除成功"); 86 request.getRequestDispatcher("main.jsp").forward(request,response); 87 }else { 88 System.out.println("删除失败"); 89 request.getRequestDispatcher("fail.jsp").forward(request,response); 90 } 91 92 } 93 public void alter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 94 response.setCharacterEncoding("UTF-8"); 95 request.setCharacterEncoding("UTF-8"); 96 97 List<User> list=dao.find(); 98 request.setAttribute("list", list); 99 request.getRequestDispatcher("alter.jsp").forward(request,response); 100 101 } 102 public void alter_show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 103 response.setCharacterEncoding("UTF-8"); 104 request.setCharacterEncoding("UTF-8"); 105 106 String name=request.getParameter("name"); 107 System.out.println("name:"+name); 108 List<User> list=dao.alter(name); 109 request.setAttribute("list", list); 110 System.out.println("进入修改界面成功"); 111 request.getRequestDispatcher("update.jsp").forward(request,response); 112 113 } 114 public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 115 response.setCharacterEncoding("UTF-8"); 116 request.setCharacterEncoding("UTF-8"); 117 118 String name=request.getParameter("name"); 119 String judge=request.getParameter("judge"); 120 String password=request.getParameter("password"); 121 String sex=request.getParameter("sex"); 122 String phone=request.getParameter("phone"); 123 124 System.out.println("name:"+name); 125 System.out.println("judge:"+judge); 126 System.out.println("password:"+password); 127 System.out.println("sex:"+sex); 128 System.out.println("phone:"+phone); 129 130 User user=new User(name,password,sex,phone); 131 132 if(dao.update(user,judge)){ 133 System.out.println("修改成功"); 134 request.getRequestDispatcher("main.jsp").forward(request,response); 135 }else { 136 System.out.println("修改失败"); 137 request.getRequestDispatcher("fail.jsp").forward(request,response); 138 } 139 140 } 141 142 143 /** 144 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 145 */ 146 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 147 // TODO Auto-generated method stub 148 response.setCharacterEncoding("UTF-8"); 149 request.setCharacterEncoding("UTF-8"); 150 System.out.println("进入Servlet"); 151 String method = request.getParameter("method"); 152 System.out.println(method); 153 if("add".equals(method)) { 154 add(request,response); 155 }else if("find".equals(method)) { 156 find(request,response); 157 }else if("delete".equals(method)) { 158 delete(request,response); 159 }else if("delete_show".equals(method)) { 160 delete_show(request,response); 161 }else if("alter".equals(method)) { 162 alter(request,response); 163 }else if("alter_show".equals(method)) { 164 alter_show(request,response); 165 }else if("update".equals(method)) { 166 update(request,response); 167 } 168 } 169 170 /** 171 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 172 */ 173 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 174 // TODO Auto-generated method stub 175 doGet(request, response); 176 } 177 178 }
User.java
1 package util; 2 3 public class User { 4 private String name;//用户名 5 private String password;//密码 6 private String sex;//性别 7 private String phone;//电话 8 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public String getPassword() { 16 return password; 17 } 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 public String getSex() { 22 return sex; 23 } 24 public void setSex(String sex) { 25 this.sex = sex; 26 } 27 public String getPhone() { 28 return phone; 29 } 30 public void setPhone(String phone) { 31 this.phone = phone; 32 } 33 34 public User() {super();} 35 public User(String name,String password,String sex,String phone) { 36 super(); 37 this.name=name; 38 this.password=password; 39 this.sex=sex; 40 this.phone=phone; 41 } 42 43 @Override 44 public String toString() { 45 return "User{" + 46 "name=" + name + 47 ", password='" + password + '\'' + 48 ", sex=" + sex + 49 ", phone='" + phone + '\'' + 50 '}'; 51 } 52 53 }
add.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>增加页面</title> 8 </head> 9 <body> 10 <form action="Servlet?method=add" method="post" > 11 <table align="center"> 12 <tr> 13 <td>姓名:</td> 14 <td> 15 <input type="text" id="name" name="name" placeholder="请输入姓名" > 16 </td> 17 </tr> 18 <tr> 19 <td>密码:</td> 20 <td> 21 <input type="password" id="password" name="password" placeholder="请输入密码"> 22 </td> 23 </tr> 24 <tr> 25 <td>性别:</td> 26 <td> 27 <input type="radio" id="sex" name="sex" checked="checked" value="男">男 28 <input type="radio" id="sex" name="sex" value="女">女 29 </td> 30 </tr> 31 <tr> 32 <td>电话:</td> 33 <td> 34 <input type="text" id="phone" name="phone" placeholder="请输入电话"> 35 </td> 36 </tr> 37 </table> 38 <table align="center"> 39 <tr style="text-align: center"> 40 <td><input type="submit" value="增加"></td> 41 <td><input type="reset" value="重置"></td> 42 <td><input type="button" value="返回" onclick="window.location.href='main.jsp'"></td> 43 </tr> 44 </table> 45 </form> 46 </body> 47 </html>
alter.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>修改界面</title> 9 </head> 10 <body> 11 <form action="Servlet?method=alter" method="post" > 12 <table align="center"> 13 <tr> 14 <th>姓名</th> 15 <th>密码</th> 16 <th>性别</th> 17 <th>电话</th> 18 </tr> 19 <c:forEach items="${list}" var="user"> 20 <tr> 21 <td>${user.name}</td> 22 <td>${user.password}</td> 23 <td>${user.sex}</td> 24 <td>${user.phone}</td> 25 <td><a href="Servlet?method=alter_show&name=${user.name}">修改</a></td> 26 </tr> 27 </c:forEach> 28 </table> 29 </form> 30 </body> 31 </html>
delete.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>删除界面</title> 9 </head> 10 <body> 11 <form action="Servlet?method=delete" method="post" > 12 <table align="center"> 13 <tr> 14 <th>姓名</th> 15 <th>密码</th> 16 <th>性别</th> 17 <th>电话</th> 18 </tr> 19 <c:forEach items="${list}" var="user"> 20 <tr> 21 <td>${user.name}</td> 22 <td>${user.password}</td> 23 <td>${user.sex}</td> 24 <td>${user.phone}</td> 25 <td><a href="Servlet?method=delete_show&name=${user.name}">删除</a></td> 26 </tr> 27 </c:forEach> 28 </table> 29 </form> 30 </body> 31 </html>
fail.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>失败</title> 8 </head> 9 <body> 10 失败 11 </body> 12 </html>
find.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>查询界面</title> 9 </head> 10 <body> 11 <form action="Servlet?method=find" method="post" > 12 <table align="center"> 13 <tr> 14 <th>姓名</th> 15 <th>密码</th> 16 <th>性别</th> 17 <th>电话</th> 18 </tr> 19 <c:forEach items="${list}" var="user"> 20 <tr> 21 <td>${user.name}</td> 22 <td>${user.password}</td> 23 <td>${user.sex}</td> 24 <td>${user.phone}</td> 25 </tr> 26 </c:forEach> 27 </table> 28 </form> 29 </body> 30 </html>
main.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>CRUD</title> 8 </head> 9 <body> 10 <table align="center"> 11 <tr style="text-align: center"> 12 <td><input type="button" value="增加" onclick="window.location.href='add.jsp'"></td> 13 </tr> 14 <tr style="text-align: center"> 15 <td><button onclick="window.location.href='Servlet?method=delete'">删除</button></td> 16 </tr> 17 <tr style="text-align: center"> 18 <td><button onclick="window.location.href='Servlet?method=alter'">修改</button></td> 19 </tr> 20 <tr style="text-align: center"> 21 <td><button onclick="window.location.href='Servlet?method=find'">查询</button></td> 22 </tr> 23 </table> 24 25 </body> 26 </html>
update.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>修改页面</title> 9 </head> 10 <body> 11 <form action="Servlet?method=update" method="post" > 12 <c:forEach items="${list}" var="user"> 13 <table align="center"> 14 <tr> 15 <td>姓名:</td> 16 <td> 17 <input type="text" id="name" name="name" value="${user.name}"> 18 <input type="hidden" id="judge" name="judge" value="${user.name}"> 19 </td> 20 </tr> 21 <tr> 22 <td>密码:</td> 23 <td> 24 <input type="text" id="password" name="password" value="${user.password}"> 25 </td> 26 </tr> 27 <tr> 28 <td>性别:</td> 29 <td> 30 <input type="text" id="sex" name="sex" value="${user.sex}"> 31 </td> 32 </tr> 33 <tr> 34 <td>电话:</td> 35 <td> 36 <input type="text" id="phone" name="phone" value="${user.phone}"> 37 </td> 38 </tr> 39 </table> 40 <table align="center"> 41 <tr style="text-align: center"> 42 <td><input type="submit" value="修改"></td> 43 </tr> 44 </table> 45 </c:forEach> 46 </form> 47 </body> 48 </html>