对于本篇需要总结的内容,量估计有点大,大家好好看哈,绝对全是干货,代码的已经运行测试,不存在问题,大家可以参考学习,下面开始本篇的内容。
1、添加数据到数据库:
如何用户在JSP页面的填写的信息输入到mysql数据库呢?其实原理很简单,就是我们在JSP页面里添加一个form表单标签,需要注意的是这里的action值填我们的用于与数据库库交互的select的地址,method标签有两个值可选:get和post,get方式的数据传输,不安全,传输的数据在地址栏可以看到,而post方式的传输就相对安全些,用户填写的数据不会显式让用户看到。设置好form表单标签后,当用户填写完表达后,点击提交,用户的填写的表单数据将会传输给我们的select,这样我们的select获得到数据后,通过调用我们之前编辑好的添加数据方法,便可以把用户的填写的数据添加到数据库。
jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" errorPage="error.html"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>添加数据</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="添加数据"> <meta http-equiv="description" content="添加数据到数据库"> </head> <body> <center> <h1>添加数据到mysql数据库</h1> <hr/> <form action="add" method="post" id="form" name="form"> <table> <tr> <td>姓名:</td><td><input type="text" name="name" /></td> </tr> <tr> <td>性别:</td><td><input type="radio" name="sex" value="男" checked="checked">男<input type="radio" name="sex" value="女">女</td> </tr> <tr> <td>年龄:</td><td><input type="text" name="year" /></td> </tr> <tr> <td>家乡:</td><td><input type="text" name="from" /></td> </tr> <tr> <td>学校:</td><td><input type="text" name="school" /></td> </tr> <tr> <td align="center"><input type="submit" value="提交" /></td><td align="center"><input type="reset" value="重置"></td> </tr> </table> </form> </center> </body> </html>
对于的select:
package com.mysql.jsp.select; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mysql.jsp.manager.ManagerMaImp; import com.mysql.jsp.sever.StudentMaImp; import com.mysql.jsp.student.Student; public class add extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);//调用doPsot()方法,把使用get方式传输的数据用doPost()方向接收 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");//设置编码方式 response.setCharacterEncoding("utf-8");//设置编码方式 //这里使用了一个登录验证 String manager_name = (String)request.getSession().getAttribute("name");//获得登录用户的用户名 String manager_password = (String)request.getSession().getAttribute("password");//获得登录用户的密码 ManagerMaImp mmi = new ManagerMaImp(); boolean login = mmi.getByName(manager_name, manager_password);//验证是否正确 if(!login){ request.setAttribute("news", "您还未登录,请先登录再进行操作"); request.getRequestDispatcher("Login.jsp").forward(request, response);//不正确跳转到登录页 }else{ String name = request.getParameter("name");//获得用户填写的姓名 String sex_ = request.getParameter("sex");//获得用户选择的性别 int sex = 1; if(sex_.equals("女")){ sex = 0; } int year = Integer.parseInt(request.getParameter("year"));//获得用户填写的年龄信息 String from = request.getParameter("from");//获得用户填写的故乡信息 String school = request.getParameter("school");//获得用户填写的学校信息 //生成一个学生对象 Student student = new Student(); student.setName(name); student.setSex(sex); student.setYear(year); student.setFrom(from); student.setSchool(school); StudentMaImp smi = new StudentMaImp(); boolean flag = smi.add(student);//调用添加方法,将数据添加到数据库 if(flag){ List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("Add.jsp"); } } } }
对于向数据库添加信息,就为大家总结完毕,加上前几篇,我想大家应该已经明白如何通过JSP来做动态网站了吧。对于这里提到的是否登录验证,下面单独聊一下。
3、登录验证:
这个功能大家应该遇到过很多,一般当我们在一些网站下载资料时,会有一个会员是否登录验证,只有你登录了才能下载,这个小功能很简单,但比较实用。下面我们就一起学习一下,这里用到了一个关键字:session,我们只需要在用户登录成功后,将用户的账户和密码通过requset方式进行一下声明,之后我们就可以在任何页面或select中得到。
声明代码:
request.getSession().setAttribute("name",name); request.getSession().setAttribute("password", password);
调用代码:
String manager_name = (String)request.getSession().getAttribute("name");//获得登录用户的用户名 String manager_password = (String)request.getSession().getAttribute("password");//获得登录用户的密码
4、遍历数据库:
添加数据我们已经学会了,下面我们一起学一下,如何把数据库中的所有数据遍历后,通过JSP页面为用户显示出来。
后台select代码:
StudentMaImp smi = new StudentMaImp(); List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response);
JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error.html"%> <%@page import="com.mysql.jsp.student.Student"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>遍历数据库</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% List<Student> list = (List<Student>)request.getAttribute("list"); %> <center> <h1>遍历数据库中的数据</h1> <hr/> <form action="get" method="post"> <table width="80%"> <tr> <td><a href="Add.jsp">添加</a></td><td> </td><td>精确查找:<input type="text" name="queding"/><%if(request.getAttribute("new1")!=null){ %><span style="color: red"><%=request.getAttribute("new1")%></span><% } %></td><td> </td><td>模糊查找:<input type="text" name="mohu"/><%if(request.getAttribute("new2")!=null){ %><span style="color: red"><%=request.getAttribute("new2")%></span><% } %></td><td><input type="submit" value="搜索"/></td> </tr> </table> </form> <table border="1" width="80%"> <TR> <TD>ID</TD><td>姓名</td><td>性别</td><td>年龄</td><td>家乡</td><td>学校</td><td colspan="2">操作</td> </TR> <% if(list.size()!=0){ for(int i=0; i<list.size(); i++){ Student student = list.get(i); %> <tr><TD><%=student.getId() %></TD><td><%=student.getName() %></td><td><%if(student.getSex()==1){ %>男<%}else{ %>女<%} %></td><td><%=student.getYear() %></td><td><%=student.getFrom() %></td><td><%=student.getSchool() %></td><td><a href="getId?id=<%=student.getId() %>">修改</a></td><td><a href="del?id=<%=student.getId() %>">删除</a></td></tr> <% } } %> </table> </center> </body> </html>
界面效果:
5、删除数据:
既然是删除操作,也就没有界面了,但我们点击删除后,select接到我们发送过去的数据信息,根据这些信息调用我们之前写好的删除方法,完成删除操作。
select代码:
public class del extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);//删除操作,发送数据的方式便是get方式,我们可以在地址栏看到我们发送过来的数据信息 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String manager_name = (String)request.getSession().getAttribute("name"); String manager_password = (String)request.getSession().getAttribute("password"); ManagerMaImp mmi = new ManagerMaImp(); boolean login = mmi.getByName(manager_name, manager_password); if(!login){ request.setAttribute("news", "您还未登录,请先登录再进行操作"); request.getRequestDispatcher("Login.jsp").forward(request, response); }else{ int id = Integer.parseInt(request.getParameter("id")); StudentMaImp smi = new StudentMaImp(); boolean flag = smi.del(id); if(flag){ List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("error.html"); } } } }
6、更新操作:
学完了添加、删除、遍历操作后,接下来我们一起来学习一下更新操作。
更新操作的JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" errorPage="error.html"%> <%@page import="com.mysql.jsp.student.Student"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>修改数据</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="修改数据"> <meta http-equiv="description" content="修改数据到数据库"> </head> <body> <%Student student = (Student)request.getAttribute("student"); %> <center> <h1>修改数据到mysql数据库</h1> <hr/> <form action="update" method="post" > <table> <tr> <!-- disabled="disabled":销毁的,select无法得到它的值 --> <!-- readonly="readonly":只读的,select可以读出值 --> <td>id:</td><td><input type="text" name="id" readonly="readonly" value="<%=student.getId() %>" /></td> </tr> <tr> <td>姓名:</td><td><input type="text" name="name" value="<%=student.getName() %>"/></td> </tr> <tr> <td>性别:</td><td><input type="radio" name="sex" value="男" <%if(student.getSex()==1){ %>checked="checked"<%} %> >男<input type="radio" name="sex" value="女" <%if(student.getSex()==0){ %>checked="checked"<%} %> >女</td> </tr> <tr> <td>年龄:</td><td><input type="text" name="year" value="<%=student.getYear() %>"/></td> </tr> <tr> <td>家乡:</td><td><input type="text" name="from" value="<%=student.getFrom() %>"/></td> </tr> <tr> <td>学校:</td><td><input type="text" name="school" value="<%=student.getSchool() %>"/></td> </tr> <tr> <td align="center"><input type="submit" value="更新" /></td><td align="center"><input type="reset" value="重置"></td> </tr> </table> </form> </center> </body> </html>
既然是更新,我们需要首先通过select获得需要更新信息的对象,然后将对象数据发送给JSP页面,代码:
public class come extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StudentMaImp smi = new StudentMaImp(); List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); } }
用户接收用户修改的数据,并进行数据更新的select:
public class update extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String manager_name = (String)request.getSession().getAttribute("name"); String manager_password = (String)request.getSession().getAttribute("password"); ManagerMaImp mmi = new ManagerMaImp(); boolean login = mmi.getByName(manager_name, manager_password); if(!login){ request.setAttribute("news", "您还未登录,请先登录再进行操作"); request.getRequestDispatcher("Login.jsp").forward(request, response); }else{ int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); String sex_ = request.getParameter("sex"); int sex = 1; if(sex_.equals("女")){ sex = 0; } int year = Integer.parseInt(request.getParameter("year")); String from = request.getParameter("from"); String school = request.getParameter("school"); Student student = new Student(); student.setId(id); student.setName(name); student.setSex(sex); student.setYear(year); student.setFrom(from); student.setSchool(school); StudentMaImp smi = new StudentMaImp(); boolean flag = smi.update(student); if(flag){ List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("error.html"); } } } }
好了,到这里对于通过JSP实现对数据库的增、删、改、遍历操作,就为大家总结完毕。下一篇:JSP页面精确搜索、模糊搜索