jsp分頁類---統(tǒng)一使用_JSP教程
推薦:解析Jsp常用技巧:圖片驗證碼思路 : 圖片驗證碼的實現(xiàn)主要的技術點是如何生成一個圖片。生成圖片可以使用java.awt包下的類來實現(xiàn)。我們先寫一個簡單的生成圖片的程序HelloImage.java。以下是代碼部分。 package com.vogoal.test; import java.awt.Color; import java.awt.Graphics; imp
一、建立數(shù)據(jù)庫分頁的類
package newsbean;
import java.sql.*;
public class DBConnection{
//這里使用ms jdbc
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//指定數(shù)據(jù)庫名/url
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localsqlserver";
private final String portNumber = "1433";
private final String databaseName= "MyBusiDB";
//String sConnStr = "jdbc:microsoft:sqlserver://localsqlserver:1433;DatabaseName=ourcompany";
private final String dbUserName = "sa";
private final String dbPassword = "111111";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
//這三個參數(shù)用于記錄翻頁
int iRowCount = 0; //返回總行數(shù)
int iPageCount = 0; //返回總頁數(shù)
int iPage = 0;
public DBConnection(){
try
{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.err.println("DBConnection():" + e.getMessage());
}
}
//構造一個連接字符串
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}
//================考慮在類中的分頁======================================
//內部設置總條數(shù)..
private void setRowCount(int irowcount)
{
this.iRowCount = irowcount;
}
//返回內部設置的總條數(shù)
public int getRowCount()
{
return this.iRowCount;
}
//內部設置總頁數(shù)..
private void setPageCount(int ipagecount)
{
this.iPageCount = ipagecount;
}
//返回內部設置的總頁數(shù)
public int getPageCount()
{
return this.iPageCount;
}
//內部設置當前頁..
private void setPage(int ipage)
{
this.iPage = ipage;
}
//返回內部設置的總頁數(shù)
public int getPage()
{
return this.iPage;
}
//顯示翻頁信息
//參數(shù):總頁數(shù),總行數(shù),當前頁
//應該考慮加一個查詢參數(shù)列表進入本方法--2007-4-12
public String showChangePage()
{
return this.iPage + "/" + this.iPageCount + ">>>";
}
//考慮了翻頁的選擇查詢
public ResultSet execQuery(String sql,int iPageSize,int iPage)
{//======iRowCount== iPageCount==iPageSize===iPage================
try
{
conn = DriverManager.getConnection(getConnectionUrl(),dbUserName,dbPassword);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
rs.last();
//獲取總行數(shù) 移動到最后,檢索當前行編號。
int iRowCount= rs.getRow();
//計算總頁數(shù) 總100行 每頁10行===〉總頁數(shù)= 11.4頁==> 12頁
int iPageCount = (iRowCount + iPageSize-1) / iPageSize;
if(iPage>iPageCount) iPage = iPageCount;
if(iPage <= 0 ) iPage = 1;
if(iPageCount>0){
//在方法體中已經(jīng)到了指定行
rs.absolute((iPage-1) * iPageSize +1);
}
//自己設置iPageCount和iRowCount,iPage,避免混淆?!
setPageCount(iPageCount);
setRowCount(iRowCount);
setPage(iPage);
//stmt.close();???
}//end try
catch(SQLException ex)
{
System.err.println("DBConnection.execQuery():" + ex.getMessage());
}//end catch
return rs;
}//end execQuery
//======================================================
//關閉數(shù)據(jù)庫
public void closeDB()
{
try
{
//System.out.println("DBConnection.closeDB( here!)" );
if(rs!=null)
{
rs.close();
rs = null;
}
else
{
System.out.println("rs closed!");
}//?????
if(stmt!=null)
{
stmt.close();
stmt = null;
}
else
{
System.out.println("stmt closed!");
}
if(conn!=null)
{
conn.close();
conn = null;
}
else
{
System.out.println("conn closed!");
}
}
catch(Exception ex)
{
//System.err.println("DBConnection.closeDB()" + ex.getMessage());
System.out.println("DBConnection.closeDB()" + ex.getMessage());
}
}
}//end Class
--------------------------------------------------------------------------------------------------------------
二、jsp程序中使用本類過程
1、<jsp:useBean id="conn" class="newsbean.DBConnection" />
2、處理查詢參數(shù)iPage ,txtSearchKeyword等
3、文章列表:
int iPageSize = 15;//每次讀行數(shù),作為參數(shù)傳入<jsp:setProperty id......沒用上!
String sql=null;
ResultSet rs =null;
int iRowCount = 0;
int iPageCount = 0;
try{
//===========================
int i = 0;
//l構造sql語句。。。
sql="SELECT top 200 number, sms_no, company_card_name, Reg_Date, Dead_Date, OnUse, province, "
+ "City FROM dbo.Company_Card where 1=1 " ;
if(!( txtSearchCompanyCard.equals("")))sql = sql + " and company_card_name like '%" + txtSearchCompanyCard + "%'";
if(!( txtSearchCity.equals("")))sql = sql + " and City ='" + txtSearchCity + "'";
sql = sql + " order by number desc ";
//取得resultset
rs = conn.execQuery(sql,iPageSize,iPage);
//獲取記錄總數(shù)
iRowCount = conn.getRowCount();
//獲取總頁數(shù)
iPageCount = conn.getPageCount();
//循環(huán)
do{
%>
<tr>
<td>[<%=rs.getRow()%>]</td>
<td><%=rs.getString("sms_no")%></td>
<td><%=rs.getString("company_card_name")%></td>
<td><%=rs.getDate("Reg_Date")%></td>
<td><%=rs.getString("province")%></td>
<td><%=rs.getString("City")%></td>
<td><a href="#" onclick="return domodify('<%=rs.getInt("number")%>')">修改</a></td>
</tr>
<%
}while(++i<iPageSize && rs.next());
}catch(Exception e){
out.print("rs Err:" + e.getMessage());
out.print(sql + "<br/>");
}%>
分享:談CSV文件的生成與分析CSV文件是指Excel可以識別的后綴名為CSV的文件,網(wǎng)站系統(tǒng)后臺用來存儲分析數(shù)據(jù)的時候有可能會用到它。其實CSV文件的讀取和做成比較簡單,主要的技術點是文件的讀寫。不過CSV文件的分析和生成有一定的代表性,可以通過對CSV文件的生成和分析的實現(xiàn),來了解后
- jsp response.sendRedirect不跳轉的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結示例
- JSP FusionCharts Free顯示圖表 具體實現(xiàn)
- 網(wǎng)頁模板:關于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網(wǎng)站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關鏈接:
- 教程說明:
JSP教程-jsp分頁類---統(tǒng)一使用
。