JSP訪問資料庫分頁顯示

來源:互聯網
上載者:User

<%@ page c import="java.sql.*" %><br /><%<br /> //變數聲明<br /> Connection sqlCon=null; //資料庫連接對象<br /> Statement sqlStmt; //SQL語句對象<br /> ResultSet sqlRst; //結果集對象<br /> String strSQL; //SQL語句 </p><p> int intPageSize; //一頁顯示的記錄數<br /> int intRowCount; //記錄總數<br /> int intPageCount; //總頁數<br /> int intPage; //待顯示頁碼<br /> String strPage;<br /> int i;<br /> // 設定一頁顯示的記錄數<br /> intPageSize = 8;<br /> // 取得待顯示頁碼<br /> strPage = request.getParameter("page");<br /> if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁資料<br /> intPage = 1;<br /> }<br /> else{//將字串轉換成整型<br /> intPage = Integer.parseInt(strPage);<br /> if(intPage<1) intPage = 1;<br /> }<br /> try<br /> {<br />Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");<br />sqlCon = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev", "sa", "");<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /> //建立一個可以滾動的唯讀SQL語句對象<br /> sqlStmt =sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);<br /> //準備SQL語句<br /> strSQL = "select id,name from personinfo order by id"; </p><p> //執行SQL語句並擷取結果集<br /> sqlRst = sqlStmt.executeQuery(strSQL);<br /> //擷取記錄總數<br /> sqlRst.last();<br /> intRowCount = sqlRst.getRow();<br /> //記算總頁數<br /> intPageCount = (intRowCount+intPageSize-1) / intPageSize;<br /> //調整待顯示的頁碼<br /> if(intPage>intPageCount) intPage = intPageCount;<br /> %> </p><p> <html><br /> <head><br /> <meta http-equiv="Content-Type" c><br /> <title>JSP資料庫操作常式-資料分頁顯示 - JDBC 2.0 - SQLServer2000</title><br /> </head> </p><p> <body> </p><p> <table border="1" cellspacing="0" cellpadding="0"><br /> <tr><br /> <th>id</th><br /> <th>name</th><br /> </tr><br /> <%<br /> if(intPageCount>0){<br /> //將記錄指標定位到待顯示頁的第一條記錄上<br /> sqlRst.absolute((intPage-1) * intPageSize + 1);<br /> //顯示資料<br /> i = 0;<br /> while(i<intPageSize && !sqlRst.isAfterLast()){<br /> %><br /> <tr><br /> <td><%=sqlRst.getString(1)%></td><br /> <td><%=sqlRst.getString(2)%></td><br /> </tr><br /> <%<br /> sqlRst.next();<br /> i++;<br /> }<br /> }<br /> %> </p><p> </table><br /> 每頁<%=intPageSize%>行<br /> 第<%=intPage%>頁 共<%=intPageCount%>頁<br><br /> <%if(intPage==1){out.print("首頁");%><br />   <br /> <%out.print("上一頁");}else{%><br /> <a href="storetest.jsp?page=1" mce_href="storetest.jsp?page=1">首頁</a>  <br /> <a href="storetest.jsp?page=<%=intPage-1%>">上一頁</a><br /> <%}%><br />   <br /> <%if(intPage==intPageCount){out.print("下一頁");%><br />   <br /> <%out.print("尾頁");}else{%><br /> <a href="storetest.jsp?page=<%=intPage+1%>">下一頁</a>  <br /> <a href="storetest.jsp?page=<%=intPageCount%>">尾頁</a><br /> <%}%><br /> 轉到第<select name="jumpPage" onchange='window.location="http://localhost:8080/chj/storetest.jsp?page="+this.value'><br /> <%<br /> for (int j=1;j<=intPageCount;j++)<br /> {<br /> if (j==intPage){<br /> %><br /> <option selected value=<%=j%>><%=j%></option><br /> <%}else{%><br /> <option value=<%=j%>><%=j%></option><br /> <%}}%><br /> </select>頁<br /> </body><br /> </html> </p><p> <%<br /> //關閉結果集<br /> sqlRst.close(); </p><p> //關閉SQL語句對象<br /> sqlStmt.close(); </p><p> //關閉資料庫<br /> sqlCon.close();<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.