Home  >  Article  >  Database  >  JAVA/JSP学习系列之六(MySQL翻页例子)_MySQL

JAVA/JSP学习系列之六(MySQL翻页例子)_MySQL

WBOY
WBOYOriginal
2016-06-01 14:08:18993browse

一、运行前准备

   下载了mysql的jdbc驱动(一个jar文件)并加载在CLASSPATH(方法见《JAVA/JSP学习系列之一(JDK安装) 》)

   (如果找不到,请从本站下载)

   建一个MySQL数据库test

   数据库中有一个表:note,字段为:name(varchar)

二、下载,安装




java.sql.Statement sqlStmt; //SQL语句对象

java.sql.ResultSet sqlRst; //结果集对象

java.lang.String strCon; //数据库连接字符串

java.lang.String strSQL; //SQL语句


int intPageSize; //一页显示的记录数

int intRowCount; //记录总数

int intPageCount; //总页数

int intPage; //待显示页码

java.lang.String strPage;

int i;

//设置一页显示的记录数

intPageSize = 2;

//取得待显示页码

strPage = request.getParameter("page");

if(strPage==null){

//表明在QueryString中没有page这一个参数,此时显示第一页数据

intPage = 1;

} else{

//将字符串转换成整型

intPage = java.lang.Integer.parseInt(strPage);

if(intPage
}

//装载JDBC驱动程序

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

//连接数据库

sqlCon= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test");

//创建语句对象

sqlStmt = sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); //执行SQL语句

strSQL = "select name from note";

//执行SQL语句并获取结果集

sqlRst = sqlStmt.executeQuery(strSQL);

//获取记录总数

sqlRst.last();

intRowCount = sqlRst.getRow();

//记算总页数

intPageCount = (intRowCount+intPageSize-1) / intPageSize;

//调整待显示的页码

if(intPage>intPageCount) intPage = intPageCount;

%>






Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn