jsp数据库库分页显示

话不多说,直接上码!!

javaee.sql

/*
 Navicat Premium Data Transfer

 Source Server         : localhost_3306
 Source Server Type    : MySQL
 Source Server Version : 80021
 Source Host           : localhost:3306
 Source Schema         : javaee

 Target Server Type    : MySQL
 Target Server Version : 80021
 File Encoding         : 65001

 Date: 17/12/2020 08:40:20
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for news_inf
-- ----------------------------
DROP TABLE IF EXISTS `news_inf`;
CREATE TABLE `news_inf`  (
  `news_id` int NOT NULL AUTO_INCREMENT,
  `news_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `news_price` int NULL DEFAULT NULL,
  `news_df` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `news_num` int NULL DEFAULT NULL,
  PRIMARY KEY (`news_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of news_inf
-- ----------------------------
INSERT INTO `news_inf` VALUES (1, 'first', 52, '北京', 1);
INSERT INTO `news_inf` VALUES (2, 'second', 54, '', 2);
INSERT INTO `news_inf` VALUES (3, 'third', 45, NULL, 3);
INSERT INTO `news_inf` VALUES (4, 'fouth', 54, NULL, NULL);
INSERT INTO `news_inf` VALUES (5, 'fifth', NULL, NULL, 5);
INSERT INTO `news_inf` VALUES (6, 'sixth', NULL, NULL, NULL);
INSERT INTO `news_inf` VALUES (7, 'seven', 45, NULL, 7);
INSERT INTO `news_inf` VALUES (8, 'eight', 45, NULL, 8);
INSERT INTO `news_inf` VALUES (9, 'nine', 45, NULL, NULL);
INSERT INTO `news_inf` VALUES (10, 'tenth', 45, NULL, 9);

SET FOREIGN_KEY_CHECKS = 1;

java代码

package p1;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class SpiltPage { 
    //定义数据库连接对象和结果集对象
    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private ResultSetMetaData rsmd = null;
    //SQL查询语句
    private String sqlStr;
    //总记录数目
    private int rowCount = 0;
    //所分的逻辑页数
    private int pageCount = 0;
    //每页显示的记录数目
    private int pageSize = 0;
    //设置参数值
    public void setCon(Connection con) {
        this.con = con;
        if(null==this.con) {
            System.out.println("Failure to get a connection");
        }else {
            System.out.println("Success to get a connection");
        }
    }

    //初始化数据库表中的信息
    public void initialize(String sqlStr,int pageSize,int ipage) {
        int irows = pageSize*(ipage-1);
        this.sqlStr = sqlStr;
        this.pageSize = pageSize;
        try {
            stmt = this.con.createStatement();
            rs = stmt.executeQuery(this.sqlStr);
            if(null != rs) {
                rs.last();
                this.rowCount = rs.getRow();
                rs.first();
                this.pageCount = (this.rowCount-1)/this.pageSize+1;
            }
            this.sqlStr = sqlStr+" limit "+irows+","+pageSize;
            stmt = this.con.createStatement();
            rs = stmt.executeQuery(this.sqlStr);
            rsmd = rs.getMetaData();
        }catch(SQLException e) {
            System.out.println("zcn数据库异常:"+e.toString());
        }
    }

    //将显示结果存到Vector
    public Vector getPage() {
        Vector vData = new Vector();
        try {
            if(null != rs) {
                while(rs.next()) {
                    String[] sData = new String[5];
                    for(int j=0;j<rsmd.getColumnCount();j++) {
                        sData[j] = rs.getString(j+1);
                    }
                    vData.addElement(sData);
                }
            }
            rs.close();
            stmt.close();
        }catch(SQLException e) {
            System.out.println("zcn数据库异常:"+e.toString());
        }
        return vData;
    }

    //获得页面总数
    public int getPageCount() {
        return this.pageCount;
    }

    //获取数据表
    public int getRowCount() {
        return this.rowCount;
    }
}

words_list_javabean.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="p1.*"%>
<jsp:useBean id="pages" scope="page" class="p1.SpiltPage"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%!
//每页显示的记录数
int pageSize = 3;
String sqlStr="";
//当前页
int showPage=1;
//数据库用户名
String userName="root";
//数据库密码
String userPassword="123456";
//数据库的URL,包括连接数据库所使用的编码格式
String url="jdbc:mysql://localhost:3306/javaee?serverTimezone=UTC&useUnicode=true&characterEncoding=gbk";
//定义连接对象
Connection dbcon;
%>
<%
try{
    //加载驱动程序
    Class.forName("com.mysql.jdbc.Driver");
    //获得数据库的连接对象
    dbcon = DriverManager.getConnection(url,userName,userPassword);
}catch(SQLException e){
    //打印出异常信息
    System.out.println(e.toString());
}

//给pages中参数con赋值
pages.setCon(dbcon);
sqlStr="select * from news_inf order by news_id";
//查询数据表,获得查询结果
String strPage = null;
//获取跳转到的目的的页面
strPage = request.getParameter("showPage");
if(null == strPage){
    showPage=1;
}else{
    try{
        showPage=Integer.parseInt(strPage);
    }catch(NumberFormatException e){
        showPage = 1;
    }

    if(showPage<1){
        showPage=1;
    }
}

pages.initialize(sqlStr, pageSize, showPage);
//获取要显示的数据集合
Vector vData = pages.getPage();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分页显示</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
    <h1 align=center>留言簿</h1>
    <div align=center>
    <table border="1" cellspacing="0" cellpadding="0" width="80%">
        <tr>
        <th width="20%">编号</th>
        <th width="20%">标题</th>
         <th width="20%">价格</th>
         <th width="20%">产地</th>
          <th width="20%">数量</th>
        </tr>
        <%
        for(int i=0;i<vData.size();i++){
            //显示数据
            String[] sData=(String[])vData.get(i);
        %>
        <tr>
            <td><%=sData[0]%></td>
            <td align=left><%=sData[1]%></td>
              <td align=left><%=sData[2]%></td>
                <td align=left><%=sData[3]%></td>
                  <td align=left><%=sData[4]%></td>
        </tr>
        <%}%>
    </table>
    <form action="words_list_javabean.jsp" mehod="get" target="_self">
        共<font color=red><%=pages.getRowCount()%></font>条&nbsp;
        <%=pageSize%>条/页&nbsp;
        第<font color=red><%=showPage%></font>页/共
        <font color=red><%=pages.getPageCount()%></font>页&nbsp;
        <a href="words_list_javabean.jsp?showPage=1" target="_self">[首页]</a>&nbsp;
        <%
            //判断“上一页”链接是否要显示
            if(showPage>1){
        %>
            <a href="words_list_javabean.jsp?showPage=<%=showPage-1%>"target="_self">[上一页]</a>&nbsp;
        <%
            }else{
        %>  
            [上一页]&nbsp;
        <%
            }
            //判断下一页链接是否显示
            if(showPage<pages.getPageCount()){
        %>
            <a href="words_list_javabean.jsp?showPage=<%=showPage+1%>"target="_self">[下一页]</a>
        <%
            }else{
        %>
            [下一页]&nbsp;
        <%} %>
        <a href="words_list_javabean.jsp?showPage=<%=pages.getPageCount()%>"target="_self">[尾页]</a>&nbsp;
        转到
        <select name="showPage">
        <%
            for(int i=1;i<=pages.getPageCount();i++){
        %>
            <option value="<%=i%>"<%if(showPage==i)out.println("selected");%>><%=i%></option>
        <%} %>  
        </select>
        页&nbsp;
        <input type="submit" name="go" value="提交"/>
    </form>
    <%
    //关闭数据库连接
    dbcon.close();
    %>
    </div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_41309350/article/details/111307438