对于本篇需要总结的内容,量估计有点大,大家好好看哈,绝对全是干货,代码的已经运行测试,不存在问题,大家可以参考学习,下面开始本篇的内容。
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+"/";%>添加数据 添加数据到mysql数据库
对于的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){ Listlist = new ArrayList (); 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(); Listlist = new ArrayList (); 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+"/";%>遍历数据库 <% Listlist = (List )request.getAttribute("list"); %> 遍历数据库中的数据
<% if(list.size()!=0){ for(int i=0; i ID 姓名 性别 年龄 家乡 学校 操作 <% } } %> <%=student.getId() %> <%=student.getName() %> <%if(student.getSex()==1){ %>男<%}else{ %>女<%} %> <%=student.getYear() %> <%=student.getFrom() %> <%=student.getSchool() %> 修改 删除
界面效果:
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){ Listlist = new ArrayList (); 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+"/";%>修改数据 <%Student student = (Student)request.getAttribute("student"); %>修改数据到mysql数据库
既然是更新,我们需要首先通过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(); Listlist = new ArrayList (); 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){ Listlist = new ArrayList (); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("error.html"); } } }}
好了,到这里对于通过JSP实现对数据库的增、删、改、遍历操作,就为大家总结完毕。下一篇:JSP页面精确搜索、模糊搜索