jsp分頁類---統一使用

來源:互聯網
上載者:User

一、建立資料庫分頁的類

package newsbean;
import java.sql.*;

public class DBConnection{
//這裡使用ms jdbc
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//指定資料庫名/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;

//這三個參數用於記錄翻頁
int iRowCount = 0; //返回總行數
int iPageCount = 0; //返回總頁數
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+";";
}

//================考慮在類中的分頁======================================
//內部設定總條數..
private void setRowCount(int irowcount)
{
this.iRowCount = irowcount;
}
//返回內部設定的總條數
public int getRowCount()
{
return this.iRowCount;
}

//內部設定總頁數..
private void setPageCount(int ipagecount)
{
this.iPageCount = ipagecount;
}
//返回內部設定的總頁數
public int getPageCount()
{
return this.iPageCount;
}

//內部設定當前頁..
private void setPage(int ipage)
{
this.iPage = ipage;
}
//返回內部設定的總頁數
public int getPage()
{
return this.iPage;
}
//顯示翻頁資訊
//參數:總頁數,總行數,當前頁
//應該考慮加一個查詢參數列表進入本方法--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();

//擷取總行數 移動到最後,檢索當前行編號。
int iRowCount= rs.getRow();
//計算總頁數 總100行 每頁10行===〉總頁數= 11.4頁==> 12頁
int iPageCount = (iRowCount + iPageSize-1) / iPageSize;
if(iPage>iPageCount) iPage = iPageCount;
if(iPage <= 0 ) iPage = 1;
if(iPageCount>0){
//在方法體中已經到了指定行
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

//======================================================
//關閉資料庫
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、處理查詢參數iPage ,txtSearchKeyword等
3、文章列表:

int iPageSize = 15;//每次讀行數,作為參數傳入<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);

//擷取記錄總數
iRowCount = conn.getRowCount();
//擷取總頁數
iPageCount = conn.getPageCount();

//迴圈
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/>");
}%>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.