開始本篇總結之前,首先聊一聊上一篇中存在的一點小問題,上上篇總結數據庫創建表時,存在一個問題,name、year、form好像屬于關鍵字,不能做為表的屬性,所以大家注意一下,在創建表時保證表的屬性不存在沖突,故而上一篇中關于sql語句的地方大家需要修改一下表的屬性名。
下面開始本篇關于JSP與MySQL的交互連接,為了方便總結,我將以創建一個學生信息管理系統為目標,本篇就以登錄功能的實現為主體進行總結。
1、創建管理員表:
2、創建一個管理員類:
public class Manager { PRivate int id; private String name ; private String passWord; private int key; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } }
3、添加用戶名、密碼判斷方法:
public class ManagerMaImp { //登錄驗證 public boolean getByName(String name, String password){ System.out.println(name+" "+password); boolean flag = false; Connection conn = null; Statement st = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from manager where user_name='"+name+"' and pwd='"+password+"' and num="+1; try { st = conn.createStatement(); rs = st.executeQuery(sql); if(rs.next()){ flag = true; } } catch (SQLException e) { e.printStackTrace(); } return flag; }}
4、設計登錄JSP頁面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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="descr效果圖:
5、用于身份判斷的select:
public class login 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 name = request.getParameter("name"); String password = request.getParameter("pwd"); ManagerMaImp mmi = new ManagerMaImp(); boolean flag = mmi.getByName(name, password); if(flag){ StudentMaImp smi = new StudentMaImp(); List<Student> list = new ArrayList<Student>(); list = smi.getAll(); request.setAttribute("list", list); request.getRequestDispatcher("All.jsp").forward(request, response); }else{ response.sendRedirect("Login.jsp"); } }}6、主界面(All.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@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"/></td><td> </td><td>模糊查找:<input type="text" name="mohu"/></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>效果圖:
本篇總結:本篇提到了select,JSP就是通過select與后臺數據庫進行交互的,我們上一篇總結的增刪改查方法,將會在接下來的幾篇一一為大家實現具體的使用。在主界面JSP代碼中,你一定看到了很多:<%%>,我們可以在<%添加JAVA代碼%>。
下一篇將系統為大家總結增、刪、改的具體操作。
新聞熱點
疑難解答