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

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.*"%>


<%!
//每页显示的记录数
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();
%>



分页显示


    

留言簿

<% for(int i=0;i <%}%>
编号 标题 价格 产地 数量
<%=sData[0]%> <%=sData[1]%> <%=sData[2]%> <%=sData[3]%> <%=sData[4]%>
<%=pages.getRowCount()%>条  <%=pageSize%>条/页  第<%=showPage%>页/共 <%=pages.getPageCount()%>页  [首页]  <% //判断“上一页”链接是否要显示 if(showPage>1){ %> [上一页]  <% }else{ %> [上一页]  <% } //判断下一页链接是否显示 if(showPage [下一页] <% }else{ %> [下一页]  <%} %> [尾页]  转到 页  <% //关闭数据库连接 dbcon.close(); %>

你可能感兴趣的:(jsp,后端)