一
package newsbean;
import java
public class DBConnection{
//這裡使用ms jdbc
String sDBDriver = "com
//指定數據庫名/url
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localsqlserver";
private final String portNumber = "
private final String databaseName= "MyBusiDB";
//String sConnStr = "jdbc:microsoft:sqlserver://localsqlserver:
private final String dbUserName = "sa";
private final String dbPassword = "
// Informs the driver to use server a side
// 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 =
int iPageCount =
int iPage =
public DBConnection(){
try
{
Class
}
catch(ClassNotFoundException e)
{
System
}
}
//構造一個連接字符串
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}
//================考慮在類中的分頁======================================
//內部設置總條數
private void setRowCount(int irowcount)
{
this
}
//返回內部設置的總條數
public int getRowCount()
{
return this
}
//內部設置總頁數
private void setPageCount(int ipagecount)
{
this
}
//返回內部設置的總頁數
public int getPageCount()
{
return this
}
//內部設置當前頁
private void setPage(int ipage)
{
this
}
//返回內部設置的總頁數
public int getPage()
{
return this
}
//顯示翻頁信息
//參數
//應該考慮加一個查詢參數列表進入本方法
public String showChangePage()
{
return this
}
//考慮了翻頁的選擇查詢
public ResultSet execQuery(String sql
{//======iRowCount== iPageCount==iPageSize===iPage================
try
{
conn = DriverManager
stmt = conn
rs = stmt
rs
//獲取總行數 移動到最後
int iRowCount= rs
//計算總頁數 總
int iPageCount = (iRowCount + iPageSize
if(iPage>iPageCount) iPage = iPageCount;
if(iPage <=
if(iPageCount>
//在方法體中已經到了指定行
rs
}
//自己設置iPageCount和iRowCount
setPageCount(iPageCount);
setRowCount(iRowCount);
setPage(iPage);
//stmt
}//end try
catch(SQLException ex)
{
System
}//end catch
return rs;
}//end execQuery
//======================================================
//關閉數據庫
public void closeDB()
{
try
{
//System
if(rs!=null)
{
rs
rs = null;
}
else
{
System
}//?????
if(stmt!=null)
{
stmt
stmt = null;
}
else
{
System
}
if(conn!=null)
{
conn
conn = null;
}
else
{
System
}
}
catch(Exception ex)
{
//System
System
}
}
}//end Class
二
int iPageSize =
String sql=null;
ResultSet rs =null;
int iRowCount =
int iPageCount =
try{
//===========================
int i =
//l構造sql語句
sql="SELECT top
+ "City FROM dbo
if(!( txtSearchCompanyCard
if(!( txtSearchCity
sql = sql + " order by number desc ";
//取得resultset
rs = conn
//獲取記錄總數
iRowCount = conn
//獲取總頁數
iPageCount = conn
//循環
do{
%>
<tr>
<td>[<%=rs
<td><%=rs
<td><%=rs
<td><%=rs
<td><%=rs
<td><%=rs
<td><a href="#" onclick="return domodify(
</tr>
<%
}while(++i<iPageSize && rs
}catch(Exception e){
out
out
}%>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20574.html