本文實例講述了JSP實現(xiàn)的簡單分頁顯示效果代碼。分享給大家供大家參考,具體如下:
%@ page contentType="text/html;charset=gb2312" %> %@ page language="java" import="java.sql.*" %> script language="javascript"> function newwin(url) { var newwin=window.open(url,"newwin","toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes,width=600,height=450"); newwin.focus(); return false; } /SCRIPT> script LANGUAGE="javascript"> function submit10() { self.location.replace("fenye1.jsp") } /SCRIPT> %//變量聲明 java.sql.Connection sqlCon; //數(shù)據(jù)庫連接對象 java.sql.Statement sqlStmt; //SQL語句對象 java.sql.ResultSet sqlRst; //結(jié)果集對象 java.lang.String strCon; //數(shù)據(jù)庫連接字符串 java.lang.String strSQL; //SQL語句 int intPageSize; //一頁顯示的記錄數(shù) int intRowCount; //記錄總數(shù) int intPageCount; //總頁數(shù) int intPage; //待顯示頁碼 java.lang.String strPage; int i; //設(shè)置一頁顯示的記錄數(shù) intPageSize = 4; //取得待顯示頁碼 strPage = request.getParameter("page"); if(strPage==null){//表明在QueryString中沒有page這一個參數(shù),此時顯示第一頁數(shù)據(jù) intPage = 1; } else{//將字符串轉(zhuǎn)換成整型 intPage = java.lang.Integer.parseInt(strPage); if(intPage1) intPage = 1; } //裝載JDBC驅(qū)動程序 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //設(shè)置數(shù)據(jù)庫連接字符串 strCon = "jdbc:odbc:heyang"; //連接數(shù)據(jù)庫 sqlCon = java.sql.DriverManager.getConnection(strCon,"sa",""); //創(chuàng)建一個可以滾動的只讀的SQL語句對象 sqlStmt = sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.Result Set.CONCUR_READ_ONLY);//準備SQL語句 strSQL = "select user_id,user_name from userinfo order by user_id desc"; //執(zhí)行SQL語句并獲取結(jié)果集 sqlRst = sqlStmt.executeQuery(strSQL); //獲取記錄總數(shù) sqlRst.last();//??光標在最后一行 intRowCount = sqlRst.getRow();//獲得當前行號 //記算總頁數(shù) intPageCount = (intRowCount+intPageSize-1) / intPageSize; //調(diào)整待顯示的頁碼 if(intPage>intPageCount) intPage = intPageCount; %> html> head> meta http-equiv="Content-Type" content="text/html; charset=gb2312"> title>會員管理/title> /head> body> form method="POST" action="fenye1.jsp"> 第%=intPage%>頁 共%=intPageCount%>頁 %if(intPageintPageCount){%>a href="fenye1.jsp?page=%=intPage+1%>">下一頁 /a>%}%> %if(intPage>1){%>a href="fenye1.jsp?page=%=intPage-1%>"> 上一頁/a>%}%> 轉(zhuǎn)到第:input type="text" name="page" size="8"> 頁 span>input class=buttonface type='submit' value='GO' name='cndok'>/span> /form> table border="1" cellspacing="0" cellpadding="0"> tr> th>用戶名/th> th width='8%'>刪除/th> /tr> % if(intPageCount>0){ //將記錄指針定位到待顯示頁的第一條記錄上 sqlRst.absolute((intPage-1) * intPageSize + 1); //顯示數(shù)據(jù) i = 0; String user_id,user_name; while(iintPageSize !sqlRst.isAfterLast()){ user_id=sqlRst.getString(1); user_name=sqlRst.getString(2); %> tr> td>%=user_id%>/td> td>%=user_name%>/td> td width='8%' align='center'>a href="delete.jsp?user_id=%=user_id%>" onClick="return newwin(this.href);">刪除/a>/td> /tr> % sqlRst.next(); i++; } } %> /table> /body> /html> % //關(guān)閉結(jié)果集 sqlRst.close(); //關(guān)閉SQL語句對象 sqlStmt.close(); //關(guān)閉數(shù)據(jù)庫 sqlCon.close(); %>
希望本文所述對大家JSP程序設(shè)計有所幫助。