model1改進(jsp+jaav實現封裝)html
一張表對應兩個類:javabean 與 beancljava
爲何不用Result來進行返回?mysql
代碼集合;sql
Login.jap數據庫
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陸界面</title> </head> <body> <form action="LoginCl.jsp" method="post"> <table > <tr><td>用戶名</td><td><input type="text" name="username"/></td></tr> <tr><td>密 碼</td><td><input type="text" name="password"/></td></tr> <tr><td><input type="submit" value="登陸" /></td><td><input type="reset" value="清空"/></td></tr> </table> </form> </body> </html>
LoginCl.japjsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.sql.*,com.wz.model.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name=request.getParameter("username"); String password=request.getParameter("password"); out.print(name+" "+password); //驗證登陸功能 //待會調用Userbean函數 UserbeanCL ubc=new UserbeanCL(); if(ubc.checkUser(name, password)){ response.sendRedirect("http://localhost:8080/practice/Welcom.jsp?pageNow=1&&user="+name+""); }else { response.sendRedirect("Login.jsp"); } %> </body> </html>
Welcom.jspide
<%@page import="java.io.PrintWriter"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.sql.*,com.wz.model.*,java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 歡迎來到新的頁面! <a href="Login.jsp">返回主頁面</a> <hr/> <% //定義分頁能夠用到的變量 int pageNow=1; String page1=request.getParameter("pageNow"); if(page!=null){ pageNow=Integer.parseInt(page1); } String name=request.getParameter("user"); out.print(name); //調用Userbean的方法(建立實例),而後調用 UserbeanCL ubc=new UserbeanCL(); int pageCount=ubc.pageCount();//!!!!!!!!!!!!!!順序很重要先有pageCount纔會先計算出來rowCount才能夠sql查詢 out.print(pageCount); ArrayList al=ubc.getUserBypage(pageNow); out.print("size="+al.size()); %> <table border="1px" > <tr><td>用戶ID</td><td>用戶名</td><td>郵箱</td><td>等級</td><td>密碼</td></tr> <% for(int i=0;i<al.size();i++){ Userbean ub=(Userbean) al.get(i); %> <tr><td><%=ub.getId()%></td><td><%=ub.getUsername()%></td><td><%=ub.getEmail()%></td><td><%=ub.getGrade()%></td><td><%=ub.getPassword()%></td></tr> <% } %> </table> <% if(pageNow!=1){ out.print("<a href='Welcom.jsp?pageNow="+(pageNow-1)+"'>上一頁</a>");} //顯示超連接 for(int i=1;i<=pageCount;i++){ out.print("<a href='Welcom.jsp?pageNow="+i+"'>["+i+"]</a>"); } if(pageNow!=pageCount){ out.print("<a href='Welcom.jsp?pageNow="+(pageNow+1)+"'>下一頁</a>");} %> <hr/> </body> </html>
UserBean.java函數
package com.wz.model; //這是一個javabean對應一個User表,表明數據 public class Userbean { private int id; private String username; private String email; private int grade; private String password; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } /** * @return the grade */ public int getGrade() { return grade; } /** * @param grade the grade to set */ public void setGrade(int grade) { this.grade = grade; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } }
UserbeanCLjavapost
package com.wz.model; import java.sql.*; import java.util.*; //這是一個處理類,主要用於對User表的封裝操做(增刪改查) //對應User表的一條記錄 public class UserbeanCL { //驗證用戶名是否存在 boolean b=false; private Connection conn = null; private PreparedStatement ps = null; private ResultSet rs = null; private int pageSize=3; private int rowCount=0; private int pageCount=0; public void close(){ try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean checkUser(String u,String p){ System.out.println("你們好"); try{ // Class.forName("com.mysql.jdbc.Driver"); // conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456"); conn=new ConnDB().getConn(); System.out.println(u); String sql = "select password from user where username=? "; ps = conn.prepareStatement(sql); ps.setString(1, u); System.out.println(sql); // 結果 rs = ps.executeQuery(); if (rs.next()) { if(rs.getString(1).equals(p)) {b=true; } } }catch (Exception e) { e.printStackTrace(); } finally {//關閉打開的資源 this.close(); } return b; } //返回頁總數 public int pageCount(){//沒毛病 try { //獲得鏈接 //計算Page conn=new ConnDB().getConn(); String sql = "select count(*) from user"; ps = conn.prepareStatement(sql); // 結果 rs = ps.executeQuery(); if (rs.next()) {//next不要忘了 rowCount=rs.getInt(1); } //計算PageCount if(rowCount%pageSize==0){ pageCount=rowCount/pageSize; }else { pageCount=rowCount/pageSize+1; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ this.close(); } return pageCount; } //實現分頁功能 public ArrayList getUserBypage(int pageNow){ String sql=""; ArrayList al=new ArrayList(); try{ System.out.println("測試"+pageNow+pageCount+pageSize+rowCount); //查詢出須要顯示的頁面 conn=new ConnDB().getConn(); if(rowCount>=pageNow*pageSize){ sql="select *from user limit "+(pageNow-1)*this.pageSize+","+this.pageSize; } else{ sql="select *from user limit "+(pageNow-1)*this.pageSize+","+(rowCount-(pageNow-1)*this.pageSize); } ps = conn.prepareStatement(sql); System.out.println(sql); // 結果 rs = ps.executeQuery(); while(rs.next()){ Userbean ub=new Userbean(); ub.setId(rs.getInt(1)); ub.setUsername(rs.getString(2)); ub.setEmail(rs.getString(3)); ub.setGrade(rs.getInt(4)); ub.setPassword(rs.getString(5)); al.add(ub); } }catch(Exception e){ e.printStackTrace(); }finally{ this.close(); } return al; } }
Conndb.java測試
package com.wz.model; //獲得數據庫的鏈接 import java.sql.*; public class ConnDB { private Connection ct=null; public Connection getConn(){ try { Class.forName("com.mysql.jdbc.Driver"); ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return ct; } }