• 纯JSP MySQL分页


    1. <%@ page contentType="text/html; charset=UTF-8" %>
    2. <%@ page language="java"%>
    3. <%@ page import="java.sql.*"%>
    4. <%!public int nullIntconvert(String str) {
    5. int num = 0;
    6. if (str == null) {
    7. str = "0";
    8. } else if ((str.trim()).equals("null")) {
    9. str = "0";
    10. } else if (str.equals("")) {
    11. str = "0";
    12. }
    13. try {
    14. num = Integer.parseInt(str);
    15. } catch (Exception e) {
    16. }
    17. return num;
    18. }%>
    19. <%
    20. Connection conn = null;
    21. Class.forName("com.mysql.jdbc.Driver").newInstance();
    22. conn = DriverManager.getConnection(
    23. "jdbc:mysql://localhost:3306/jpetstore", "root", "root");
    24. ResultSet rs1 = null;
    25. ResultSet rs2 = null;
    26. PreparedStatement ps1 = null;
    27. PreparedStatement ps2 = null;
    28. int showRows = 2;
    29. int totalRecords = 5;
    30. int totalRows = nullIntconvert(request.getParameter("totalRows"));
    31. int totalPages = nullIntconvert(request.getParameter("totalPages"));
    32. int iPageNo = nullIntconvert(request.getParameter("iPageNo"));
    33. int cPageNo = nullIntconvert(request.getParameter("cPageNo"));
    34. int startResult = 0;
    35. int endResult = 0;
    36. if (iPageNo == 0) {
    37. iPageNo = 0;
    38. } else {
    39. iPageNo = Math.abs((iPageNo - 1) * showRows);
    40. }
    41. String query1 = "SELECT SQL_CALC_FOUND_ROWS * FROM jpetstore.item LIMIT "
    42. + iPageNo + "," + showRows + "";
    43. ps1 = conn.prepareStatement(query1);
    44. rs1 = ps1.executeQuery();
    45. String query2 = "SELECT FOUND_ROWS() as cnt";
    46. ps2 = conn.prepareStatement(query2);
    47. rs2 = ps2.executeQuery();
    48. if (rs2.next()) {
    49. totalRows = rs2.getInt("cnt");
    50. }
    51. %>
    52. <html>
    53. <h3>Pagination of JSP pageh3>
    54. <body>
    55. <form>
    56. <input type="hidden" name="iPageNo" value="<%=iPageNo%>"> <input
    57. type="hidden" name="cPageNo" value="<%=cPageNo%>"> <input
    58. type="hidden" name="showRows" value="<%=showRows%>">
    59. <table width="100%" cellpadding="0" cellspacing="0" border="1">
    60. <tr>
    61. <td>ItemIDtd>
    62. <td>ProductIDtd>
    63. <td>listPricetd>
    64. <td>unitCosttd>
    65. tr>
    66. <%
    67. while (rs1.next()) {
    68. %>
    69. <tr>
    70. <td><%=rs1.getString("itemid")%>td>
    71. <td><%=rs1.getString("productId")%>td>
    72. <td><%=rs1.getDouble("listprice")%>td>
    73. <td><%=rs1.getDouble("unitcost")%>td>
    74. tr>
    75. <%
    76. }
    77. %>
    78. table>
    79. <%
    80. try {
    81. if (totalRows < (iPageNo + showRows)) {
    82. endResult = totalRows;
    83. } else {
    84. endResult = (iPageNo + showRows);
    85. }
    86. startResult = (iPageNo + 1);
    87. totalPages = ((int) (Math.ceil((double) totalRows / showRows)));
    88. } catch (Exception e) {
    89. e.printStackTrace();
    90. }
    91. %>
    92. <div>
    93. <%
    94. int i = 0;
    95. int cPage = 0;
    96. if (totalRows != 0) {
    97. cPage = ((int) (Math.ceil((double) endResult
    98. / (totalRecords * showRows))));
    99. int prePageNo = (cPage * totalRecords)
    100. - ((totalRecords - 1) + totalRecords);
    101. if ((cPage * totalRecords) - (totalRecords) > 0) {
    102. %>
    103. <a
    104. href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>">
    105. < Previousa>
    106. <%
    107. }
    108. for (i = ((cPage * totalRecords) - (totalRecords - 1)); i <= (cPage * totalRecords); i++) {
    109. if (i == ((iPageNo / showRows) + 1)) {
    110. %>
    111. <a href="pagination.jsp?iPageNo=<%=i%>"
    112. style="cursor: pointer; color: red"><b><%=i%>b> a>
    113. <%
    114. } else if (i <= totalPages) {
    115. %>
    116. <a href="pagination.jsp?iPageNo=<%=i%>"><%=i%>a>
    117. <%
    118. }
    119. }
    120. if (totalPages > totalRecords && i < totalPages) {
    121. %>
    122. <a href="pagination.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>">
    123. Next >a>
    124. <%
    125. }
    126. }
    127. %>
    128. <b>Rows <%=startResult%> - <%=endResult%> Total Rows <%=totalRows%>
    129. b>
    130. div>
    131. form>
    132. body>
    133. html>
    134. <%
    135. try {
    136. if (ps1 != null) {
    137. ps1.close();
    138. }
    139. if (rs1 != null) {
    140. rs1.close();
    141. }
    142. if (ps2 != null) {
    143. ps2.close();
    144. }
    145. if (rs2 != null) {
    146. rs2.close();
    147. }
    148. if (conn != null) {
    149. conn.close();
    150. }
    151. } catch (Exception e) {
    152. e.printStackTrace();
    153. }
    154. %>
    1. create table if not exists item (
    2. itemid varchar(10) not null,
    3. productid varchar(10) not null,
    4. listprice decimal(10,2) null,
    5. unitcost decimal(10,2) null,
    6. supplier int null,
    7. status varchar(2) null,
    8. attr1 varchar(80) null,
    9. attr2 varchar(80) null,
    10. attr3 varchar(80) null,
    11. attr4 varchar(80) null,
    12. attr5 varchar(80) null,
    13. primary key (itemid) )
    1. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-1','FI-SW-01',16.50,10.00,1,'P','Large');
    2. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-2','FI-SW-01',16.50,10.00,1,'P','Small');
    3. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-3','FI-SW-02',18.50,12.00,1,'P','Toothless');
    4. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-4','FI-FW-01',18.50,12.00,1,'P','Spotted');
    5. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-5','FI-FW-01',18.50,12.00,1,'P','Spotless');
    6. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-6','K9-BD-01',18.50,12.00,1,'P','Male Adult');
    7. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-7','K9-BD-01',18.50,12.00,1,'P','Female Puppy');
    8. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-8','K9-PO-02',18.50,12.00,1,'P','Male Puppy');
    9. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-9','K9-DL-01',18.50,12.00,1,'P','Spotless Male Puppy');
    10. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-10','K9-DL-01',18.50,12.00,1,'P','Spotted Adult Female');
    11. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-11','RP-SN-01',18.50,12.00,1,'P','Venomless');
    12. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-12','RP-SN-01',18.50,12.00,1,'P','Rattleless');
    13. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-13','RP-LI-02',18.50,12.00,1,'P','Green Adult');
    14. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-14','FL-DSH-01',58.50,12.00,1,'P','Tailless');
    15. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-15','FL-DSH-01',23.50,12.00,1,'P','With tail');
    16. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-16','FL-DLH-02',93.50,12.00,1,'P','Adult Female');
    17. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-17','FL-DLH-02',93.50,12.00,1,'P','Adult Male');
    18. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-18','AV-CB-01',193.50,92.00,1,'P','Adult Male');
    19. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-19','AV-SB-02',15.50, 2.00,1,'P','Adult Male');
    20. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-20','FI-FW-02',5.50, 2.00,1,'P','Adult Male');
    21. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-21','FI-FW-02',5.29, 1.00,1,'P','Adult Female');
    22. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-22','K9-RT-02',135.50, 100.00,1,'P','Adult Male');
    23. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-23','K9-RT-02',145.49, 100.00,1,'P','Adult Female');
    24. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-24','K9-RT-02',255.50, 92.00,1,'P','Adult Male');
    25. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-25','K9-RT-02',325.29, 90.00,1,'P','Adult Female');
    26. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-26','K9-CW-01',125.50, 92.00,1,'P','Adult Male');
    27. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-27','K9-CW-01',155.29, 90.00,1,'P','Adult Female');
    28. INSERT INTO item (itemid, productid, listprice, unitcost, supplier, status, attr1) VALUES ('EST-28','K9-RT-01',155.29, 90.00,1,'P','Adult Female');

     

     

     

     

  • 相关阅读:
    电商大促就靠RPA,摆脱重复劳动,急速提效
    python自动化测试面试题(三)(持续更新)
    JAVA常见基础面试问题汇集
    开发工程师必备————【Day09】数据库基础知识与基本SQL语句
    STM32单片机IC卡指纹密码锁门禁系统GSM短信报警
    实现放大镜的效果
    GPFS 文件系统部署步骤
    ❤mac使用Idea工具
    详解C++三大特性之继承
    vue 项目的屏幕自适应方案
  • 原文地址:https://blog.csdn.net/allway2/article/details/126316275