第一個檔案PagedbClass.java
代碼如下 |
複製代碼 |
package com.kanba.tools; import java.sql.*; import com.kanba.connection.Conn; public class PagedbClass { Connection con = null; Statement stmt = null; ResultSet rs = null; ResultSetMetaData resultsMeta =null; int rows = 0; public PagedbClass() { con = new Conn().getConnection(); } public ResultSet executeQuery(String sql) throws SQLException{ ResultSet rs = null; try{ stmt = con.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()) this.rows ++; rs = stmt.executeQuery(sql); } catch (SQLException e){ System.out.print("Query:"+e.getMessage()); } this.rs = rs; return rs; } public boolean executeUpdate(String sql){ try{ stmt = con.createStatement(); stmt.executeUpdate(sql); return true; } catch(SQLException e){ System.out.print("Update:"+e.getMessage()); return false; } } public int getColumns(){ int columns = 0; try{ this.resultsMeta = this.rs.getMetaData(); columns = this.resultsMeta.getColumnCount(); } catch (SQLException e) {} return columns; } public int getRows(){ return this.rows; } public void closedb(){ try{ con.close(); }catch(SQLException e){ e.printStackTrace(); } } } |
第二個檔案分頁通用類 PageQuery.java
代碼如下 |
複製代碼 |
package com.kanba.tools; import java.sql.*; import javax.servlet.http.*; public class PageQuery { int Offset; // 記錄位移量 int Total; // 記錄總數 int MaxLine; // 記錄每頁顯示記錄數 ResultSet rs; // 讀出的結果 int TPages; // 總頁數 int CPages; // 當前頁數 String PageQuery; // 分頁顯示要傳遞的參數 String Query; // query 語句 String QueryPart; // " FROM " 以後的 query 部分 String FilePath; PagedbClass db; // object of dbclass //constructer do nothing public PageQuery() { // 每頁顯示十行 MaxLine = 30; db = new PagedbClass(); } //********讀取記錄*************** // 主要工作函數,根據所給的條件從表中讀取相應的記錄 public ResultSet myQuery(String query, HttpServletRequest req) throws SQLException { String query_part, os; int begin, offset; // 截取 " FROM " 以後的 query 語句 begin = query.indexOf(" FROM "); query_part = query.substring(begin, query.length()).trim(); // 計算位移量 os = req.getParameter("offset"); if (os == null) Offset = 0; else Offset = Integer.parseInt(os); // 擷取檔案名稱 FilePath = req.getRequestURI(); Query = query; QueryPart = query_part; // 計算總的記錄條數 String SQL = "SELECT Count(*) AS total " + this.QueryPart; rs = db.executeQuery(SQL); if (rs.next()) Total = rs.getInt(1); // 設定當前頁數和總頁數 TPages = (int)Math.ceil((double)this.Total/this.MaxLine); CPages = (int)Math.floor((double)Offset/this.MaxLine+1); // 根據條件判斷,取出所需記錄 if (Total > 0) { SQL = Query + " LIMIT " + Offset + " , " + MaxLine; rs = db.executeQuery(SQL); } return rs; } public void close(){ db.closedb(); } // 顯示總頁數 public int getTotalPages() { return TPages; } //顯示當前所在頁數 public int getCurrenPages() { return CPages; } //**********顯示翻頁提示欄************* // 顯示首頁、下頁、上頁、尾頁 public String PageLegend() { String str = ""; int first, next, prev, last; first = 0; next = Offset + MaxLine; prev = Offset - MaxLine; last = (this.TPages - 1) * MaxLine; if(Offset >= MaxLine) str += " <A href=" + FilePath + "?offset=" + first + ">首頁</A> "; else str += " 首頁 "; if(prev >= 0) str += " <A href=" + FilePath + "?offset=" + prev + ">前頁</A> "; else str += " 前頁 "; if(next < Total) str += " <A href=" + FilePath + "?offset=" + next + ">後頁</A> "; else str += " 後頁 "; if(TPages != 0 && CPages < TPages) str += " <A href=" + FilePath + "?offset=" + last + ">尾頁</A>"; else str += " 尾頁 "; str += " 頁次:" + getCurrenPages() + "/" + getTotalPages() + "頁 "; str += MaxLine + "條/頁 " + "共" + Total + "條"; String pageNum; return str; } }
|
資料庫連接檔案 Conn.java
代碼如下 |
複製代碼 |
package com.kanba.connection import java.sql.*; public class Conn { public Conn(){} //這個是串連資料庫的代碼,也可以通過串連池等技術實現。 String driverClass="org.gjt.mm.mysql.Driver"; String url = "jdbc:mysql://localhost/test?user=root&password=0&useUnicode=true&characterEncoding=gb2312"; String username="root"; String password="pwd"; Connection conn=null; //初始化 public Connection getConnection() { try { Class.forName(driverClass); //載入驅動 // System.out.println("jiazaichenggong"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn=DriverManager.getConnection(url); //建立串連conn } catch (SQLException e) { e.printStackTrace(); } return conn; } } 分頁測試檔案 page.jsp <%@ page language="java" import="java.sql.*, com.kanba.tools.*" %> <%@ page contentType="text/html; charset=gb2312" %> <jsp:useBean id="pq" scope="page" class="com.kanba.tools.PageQuery" /> <html> <body bgcolor="#8BA9C9"> <table bgcolor="#fecda9" cellspacing=0> <% String query = "SELECT * FROM users"; // 注意這個" FROM "一定要大寫,程式中需要靠這個FROM後的query語句判斷總行數。 ResultSet rs = pq.myQuery(query, request); String bar = pq.PageLegend(); //讀取分頁提示欄 out.println("<tr><td colspan=2>"+bar+"</td></tr>"); out.println("<tr><td colspan=2><hr size=1 color=blue></td></tr>"); while (rs.next()) { //說明:rs.getString(2)等是列名相對應的位置,不要用rs.getString("colmn_name")這種形式 %> <tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td></tr> <% } rs.close;pq.close(); %> </table> </body> </html> |