运用MVC对数据进行一些简单的处理,基本实现数据的增删改查,达到前端和后台的数据之间的交互。
1.开始界面
1 <%@page import="com.zdsofe.work.Student"%> 2 <%@page import="java.util.List"%> 3 <%@page import="com.zdsofe.work.ReadData"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 <script type="text/javascript" src="../js/jquery-1.7.2.js"></script> 12 <script type="text/javascript"> 13 function fun1() { 14 $(":checkbox").attr("checked","checked"); 15 } 16 17 function fun2() { 18 $(":checkbox").attr("checked",false); 19 } 20 21 </script> 22 </head> 23 <body> 24 <% ReadData rd=new ReadData(); 25 List<Student> list= rd.findInfo(); 26 27 %> 28 <form action="../MoreDeleteServlet" > 29 <table border="1"> 30 <tr> <td colspan="4"><button type="button" οnclick="fun1()">全选</button> 31 <button type="button" οnclick="fun2()">全不选</button></td></tr> 32 <tr> 33 <th>序号</th> 34 <th>名字</th> 35 <th>密码</th> 36 <th>操作</th> 37 </tr> 38 39 40 <% 41 for(int i=0;i<list.size();i++) 42 { 43 %> 44 <tr> 45 <td><%=list.get(i).getId()%></td> 46 <td><%=list.get(i).getUserName()%></td> 47 <td><%=list.get(i).getMima()%></td> 48 <td> 49 <input type="checkbox" name="c" value="<%=list.get(i).getId()%>"/> 50 <a href="../servlet?userI=<%=list.get(i).getId()%>">修改</a> 51 <a href="../DeleteServlet?userI=<%=list.get(i).getId()%>">删除</a> 52 </td> 53 </tr> 54 <% 55 } 56 57 %> 58 <tr> 59 <td colspan="4"><a href="addData.jsp" style="text-decoration: none;">增加</a> 60 <button type="submit">批量删除</button> 61 </td> 62 </tr> 63 </table> 64 </form> 65 <form action="../DimServlet"> 66 <select name="se"> 67 <option value="id">序号</option> 68 <option value="name">姓名</option> 69 </select> 70 <input type="text" name="in"/> 71 <button type="submit">模糊查询</button> 72 </form> 73 </body> 74 75 </html>
2.增加数据的界面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="../AddServlet" method="post"> 11 12 用户名:<input type="text" name="userName" ><br/> 13 密码:<input type="password" name="mima" ><br/> 14 ID:<input type="text" name="id" /> 15 <button type="submit">提交</button> 16 17 </form> 18 </body> 19 </html>
3.修改数据的界面
1 <%@page import="com.zdsofe.work.Student"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 11 <% 12 //获取用户信息 13 Student stu=(Student)session.getAttribute("user"); 14 %> 15 <body> 16 17 <form action="../EditServlet" method="post"> 18 19 用户名:<input type="text" name="userName" value="<%=stu.getUserName()%>"><br/> 20 密码:<input type="password" name="mima" value="<%=stu.getMima()%>"><br/> 21 ID:<input type="text" name="id" value="<%=stu.getId()%>"/> 22 <button type="submit">提交</button> 23 24 </form> 25 26 </body> 27 </html>
4.模糊查询后的界面
1 <%@page import="com.zdsofe.work.Student"%> 2 <%@page import="java.util.List"%> 3 <%@page import="com.zdsofe.work.ReadData"%> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <% ReadData rd=new ReadData(); 13 String se=session.getAttribute("se").toString(); 14 String in=session.getAttribute("in").toString(); 15 List<Student> list= rd.findDim(se,in); 16 17 %> 18 <body> 19 <table border="1"> 20 <tr> 21 <th>序号</th> 22 <th>名字</th> 23 <th>密码</th> 24 <th>操作</th> 25 </tr> 26 27 <% 28 for(int i=0;i<list.size();i++) 29 { 30 %> 31 <tr> 32 <td><%=list.get(i).getId()%></td> 33 <td><%=list.get(i).getUserName()%></td> 34 <td><%=list.get(i).getMima()%></td> 35 36 <td> 37 <a href="../servlet?userI=<%=list.get(i).getId()%>">修改</a> 38 <a href="../DeleteServlet?userI=<%=list.get(i).getId()%>">删除</a> 39 </td> 40 </tr> 41 42 <% 43 } 44 45 46 %> 47 <tr> 48 <td colspan="4"><a href="addData.jsp">增加</a> 49 50 </td> 51 </tr> 52 </table> 53 </body> 54 </html>
5.连接数据库的工具类
1 package com.zdsofe.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DBUtil { 8 private static String DRIVER="com.mysql.jdbc.Driver"; 9 private static String URL="jdbc:mysql://localhost:3306/mysql"; 10 private static String user="root"; 11 private static String key="775297"; 12 public static Connection conn; 13 14 //加载驱动 15 static{ 16 try { 17 Class.forName(DRIVER); 18 } catch (ClassNotFoundException e) { 19 e.printStackTrace(); 20 } 21 } 22 //连接数据库 23 public static Connection getConnection(){ 24 try { 25 conn = DriverManager.getConnection(URL, user, key); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } 29 return conn; 30 } 31 }
6.实体类
1 package com.zdsofe.work; 2 3 public class Student { 4 public String id; 5 public String userName; 6 public String mima; 7 8 public Student(String id, String userName, String mima) { 9 super(); 10 this.id = id; 11 this.userName = userName; 12 this.mima = mima; 13 } 14 15 public Student(String userName, String mima) { 16 17 this.userName = userName; 18 this.mima = mima; 19 } 20 21 public Student() { 22 super(); 23 } 24 25 public String getId() { 26 return id; 27 } 28 public void setId(String id) { 29 this.id = id; 30 } 31 public String getUserName() { 32 return userName; 33 } 34 public void setUserName(String userName) { 35 this.userName = userName; 36 } 37 public String getMima() { 38 return mima; 39 } 40 public void setMima(String mima) { 41 this.mima = mima; 42 } 43 44 45 46 47 }
7.一些实现功能的静态方法
1 package com.zdsofe.work; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import com.zdsofe.util.DBUtil; 10 11 12 13 public class ReadData { 14 15 //查询信息 16 public static List<Student> findInfo() { 17 List <Student>list=new ArrayList<>(); 18 //调用连接并创建SQL语句 19 try { 20 Statement stam= DBUtil.getConnection().createStatement(); 21 String sql="SELECT id,userName,mima FROM denglu;"; 22 ResultSet rs=stam.executeQuery(sql); 23 24 while(rs.next()) 25 { 26 String id=rs.getString("id"); 27 String userName=rs.getString("userName"); 28 String mima=rs.getString("mima"); 29 Student stu=new Student(id,userName,mima); 30 list.add(stu); 31 } 32 33 } catch (SQLException e) { 34 e.printStackTrace(); 35 } 36 37 return list; 38 } 39 40 41 //模糊查询 42 public static List<Student> findDim(String se,String in ) { 43 List <Student>list=new ArrayList<>(); 44 45 if(se.equals("id")) 46 { //调用连接并创建SQL语句 47 try { 48 Statement stam= DBUtil.getConnection().createStatement(); 49 String sql="select * from denglu where id like '%"+in+"%'"; 50 ResultSet rs=stam.executeQuery(sql); 51 while(rs.next()) 52 { 53 String id=rs.getString("id"); 54 String userName=rs.getString("userName"); 55 String mima=rs.getString("mima"); 56 Student stu=new Student(id,userName,mima); 57 list.add(stu); 58 } 59 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } 63 } 64 if(se.equals("name")) 65 { 66 try { 67 Statement stam= DBUtil.getConnection().createStatement(); 68 String sql="select * from denglu where userName like '%"+in+"%'"; 69 70 ResultSet rs=stam.executeQuery(sql); 71 72 while(rs.next()) 73 { 74 String id=rs.getString("id"); 75 String userName=rs.getString("userName"); 76 String mima=rs.getString("mima"); 77 Student stu=new Student(id,userName,mima); 78 list.add(stu); 79 } 80 81 } catch (SQLException e) { 82 e.printStackTrace(); 83 } 84 } 85 86 return list; 87 } 88 /* 根据用户名查询用户信息 89 * @param userName 90 * @return 91 */ 92 public static Student findUserByName(String id) 93 { 94 Student stu=null; 95 //输出查询sql 96 String sql = "select * from denglu t where t.id='"+id+"'"; 97 try { 98 //调用连接并创建SQL语句 99 Statement stam= DBUtil.getConnection().createStatement(); 100 ResultSet rs=stam.executeQuery(sql); 101 if(rs.next()) 102 { 103 String i=rs.getString("id"); 104 String name=rs.getString("userName"); 105 String mima=rs.getString("mima"); 106 stu=new Student(i,name,mima); 107 } 108 109 } catch (SQLException e) { 110 e.printStackTrace(); 111 112 } 113 return stu; 114 } 115 116 /** 117 * 根据编码修改用户信息 118 * @param user 119 * @return 120 */ 121 public static int update(Student stu) 122 { 123 //执行sql的结果 124 int result = 0; 125 //更新sql 126 127 String sql = "update denglu t set t.userName='"+stu.getUserName()+"',t.mima = '"+stu.getMima()+"' where t.id ='"+stu.getId()+"'"; 128 try { 129 Statement stam= DBUtil.getConnection().createStatement(); 130 result=stam.executeUpdate(sql); 131 132 } catch (SQLException e) { 133 e.printStackTrace(); 134 } 135 136 137 return result; 138 } 139 }
8.查询数据的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import javax.servlet.http.HttpSession; 14 15 import com.zdsofe.util.DBUtil; 16 17 /** 18 * Servlet implementation class servlet 19 */ 20 @WebServlet("/servlet") 21 public class servlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * @see HttpServlet#HttpServlet() 26 */ 27 public servlet() { 28 super(); 29 // TODO Auto-generated constructor stub 30 } 31 32 /** 33 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 //解决乱码问题 37 request.setCharacterEncoding("utf-8"); 38 response.setCharacterEncoding("utf-8"); 39 response.setContentType("text/html charset=utf-8"); 40 41 //获取用户id: 42 String userI = request.getParameter("userI"); 43 44 //根据用户id查询某一条用户信息 45 Student stu=ReadData.findUserByName(userI); 46 HttpSession session = request.getSession(); 47 session.setAttribute("user", stu); 48 session.setAttribute("title", "修改用户"); 49 response.sendRedirect(request.getContextPath()+"/pages/edit.jsp"); 50 } 51 52 /** 53 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 54 */ 55 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 56 57 58 } 59 60 }
9..增加数据的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.zdsofe.util.DBUtil; 14 15 /** 16 * Servlet implementation class AddServlet 17 */ 18 @WebServlet("/AddServlet") 19 public class AddServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public AddServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 } 36 37 /** 38 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 39 */ 40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 41 int i=0; 42 43 //请求和响应页面的编码格式修改 44 request.setCharacterEncoding("UTF-8"); 45 response.setCharacterEncoding("utf-8"); 46 response.setContentType("text/html charset=utf-8"); 47 48 String sql="insert into denglu values('"+request.getParameter("id")+"','"+request.getParameter("userName")+"','"+request.getParameter("mima")+"')"; 49 try { 50 Statement stam=DBUtil.getConnection().createStatement(); 51 i=stam.executeUpdate(sql); 52 } catch (SQLException e) { 53 e.printStackTrace(); 54 } 55 if(i==1) 56 { 57 response.sendRedirect(request.getContextPath()+"/pages/student.jsp"); 58 } 59 } 60 61 }
10.删除数据的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.zdsofe.util.DBUtil; 14 15 /** 16 * Servlet implementation class DeleteServlet 17 */ 18 @WebServlet("/DeleteServlet") 19 public class DeleteServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public DeleteServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 35 //获取要删除的用户ID 36 String userId = request.getParameter("userI"); 37 //删除的sql语句 38 String sql = "delete from denglu where id = "+userId+""; 39 40 try { 41 Statement stam= DBUtil.getConnection().createStatement(); 42 stam.executeUpdate(sql); 43 } catch (SQLException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 response.sendRedirect(request.getContextPath()+"/pages/student.jsp"); 48 } 49 50 /** 51 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 52 */ 53 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 54 // TODO Auto-generated method stub 55 } 56 57 }
11.模糊查询的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import com.zdsofe.util.DBUtil; 15 16 /** 17 * Servlet implementation class DimServlet 18 */ 19 @WebServlet("/DimServlet") 20 public class DimServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public DimServlet() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 36 //请求和响应页面的编码格式修改 37 request.setCharacterEncoding("UTF-8"); 38 response.setCharacterEncoding("utf-8"); 39 response.setContentType("text/html charset=utf-8"); 40 41 //获取输入的模糊数据 42 String se=request.getParameter("se"); 43 String in=request.getParameter("in"); 44 45 ReadData.findDim(se,in); 46 request.getSession().setAttribute("se", se); 47 request.getSession().setAttribute("in", in); 48 49 /* request.getRequestDispatcher("/pages/newStudent.jsp").forward(request, response);*/ 50 response.sendRedirect(request.getContextPath()+"/pages/newStudent.jsp"); 51 } 52 53 54 /** 55 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 56 */ 57 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 58 59 } 60 61 }
12.修改数据的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.lang.reflect.InvocationTargetException; 5 import java.util.Enumeration; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import org.apache.commons.beanutils.BeanUtils; 16 17 /** 18 * Servlet implementation class EditServlet 19 */ 20 @WebServlet("/EditServlet") 21 public class EditServlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * @see HttpServlet#HttpServlet() 26 */ 27 public EditServlet() { 28 super(); 29 // TODO Auto-generated constructor stub 30 } 31 32 /** 33 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 //请求和响应页面的编码格式修改 37 38 request.setCharacterEncoding("UTF-8"); 39 response.setCharacterEncoding("utf-8"); 40 response.setContentType("text/html charset=utf-8"); 41 /*//获取表单中的所用控件name属性 42 Enumeration<String> en = request.getParameterNames(); 43 Map<String, Object> mapObj = new HashMap<>(); 44 //实例化一个Student对象 45 Student stu=new Student(); 46 while(en.hasMoreElements()) 47 { 48 String rs = en.nextElement(); 49 mapObj.put(rs, request.getParameter("rs")); 50 } 51 try { 52 BeanUtils.populate(stu, mapObj); 53 54 55 } catch (IllegalAccessException | InvocationTargetException e) { 56 e.printStackTrace(); 57 }*/ 58 String name=request.getParameter("userName"); 59 String mima=request.getParameter("mima"); 60 String id=request.getParameter("id"); 61 62 Student stu=new Student(id,name,mima); 63 //根据条件修改用户信息,调用执行sql方法 64 65 int upResult = ReadData.update(stu); 66 67 if(upResult==1) 68 { 69 response.sendRedirect(request.getContextPath()+"/pages/student.jsp"); 70 } 71 72 } 73 74 /** 75 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 76 */ 77 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 78 doGet(request, response); 79 } 80 81 }
13.删除多个的servlet
1 package com.zdsofe.work; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.zdsofe.util.DBUtil; 14 15 /** 16 * Servlet implementation class MoreDeleteServlet 17 */ 18 @WebServlet("/MoreDeleteServlet") 19 public class MoreDeleteServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public MoreDeleteServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 35 String[] id=request.getParameterValues("c"); 36 37 if(id.length>0) 38 { 39 for(int i=0;i<id.length;i++) 40 { 41 //删除的sql语句 42 String sql = "delete from denglu where id = "+id[i]+""; 43 try { 44 Statement stam= DBUtil.getConnection().createStatement(); 45 stam.executeUpdate(sql); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } 49 50 } 51 } 52 response.sendRedirect(request.getContextPath()+"/pages/student.jsp"); 53 } 54 55 /** 56 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 57 */ 58 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 59 // TODO Auto-generated method stub 60 } 61 62 }