JDBC-ODBC翻頁(yè)例子
2020-06-01 23:30:12
供稿:網(wǎng)友
一、運(yùn)行前準(zhǔn)備
建議了一個(gè)MS SQLServer7數(shù)據(jù)庫(kù) DNS,名稱為:Test_DB
數(shù)據(jù)庫(kù)中有一個(gè)表:guestbook字段為:name(varchar),email(varchar),body(text)
數(shù)據(jù)庫(kù)用戶為sa 密碼空,可以自己修改的。
二、代碼
<%@ page contentType="text/html;charset=gb2312"%>
<%
//變量聲明
java.sql.Connection sqlCon; //數(shù)據(jù)庫(kù)連接對(duì)象
java.sql.Statement sqlStmt; //SQL語(yǔ)句對(duì)象
java.sql.ResultSet sqlRst; //結(jié)果集對(duì)象
java.lang.String strCon; //數(shù)據(jù)庫(kù)連接字符串
java.lang.String strSQL; //SQL語(yǔ)句
int intPageSize; //一頁(yè)顯示的記錄數(shù)
int intRowCount; //記錄總數(shù)
int intPageCount; //總頁(yè)數(shù)
int intPage; //待顯示頁(yè)碼
java.lang.String strPage;
int i,j,k; //設(shè)置一頁(yè)顯示的記錄數(shù)
intPageSize = 5; //取得待顯示頁(yè)碼
strPage = request.getParameter("page");
if(strPage==null){
//表明在QueryString中沒(méi)有page這一個(gè)參數(shù),此時(shí)顯示第一頁(yè)數(shù)據(jù)
intPage = 1;
} else{
//將字符串轉(zhuǎn)換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1; }
//裝載JDBC-ODBC驅(qū)動(dòng)程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//設(shè)置數(shù)據(jù)庫(kù)連接字符串
strCon = "jdbc:odbc:Test_DB";
//連接數(shù)據(jù)庫(kù)
sqlCon = java.sql.DriverManager.getConnection(strCon,"sa","");
//創(chuàng)建SQL語(yǔ)句對(duì)象
sqlStmt = sqlCon.createStatement();
//獲取記錄總數(shù)
strSQL = "select count(*) from guestbook";
sqlRst = sqlStmt.executeQuery(strSQL);
//執(zhí)行SQL語(yǔ)句并取得結(jié)果集
sqlRst.next(); //記錄集剛打開的時(shí)候,指針位于第一條記錄之前
intRowCount = sqlRst.getInt(1);
sqlRst.close(); //關(guān)閉結(jié)果集
//記算總頁(yè)數(shù)
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//調(diào)整待顯示的頁(yè)碼 if(intPage>intPageCount) intPage = intPageCount;