数据库分页读取之四

<%@ page language="java" import="java.sql.*"
	contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<jsp:useBean id="liuliking" scope="page" class="net.liulixin.ku.LianJie" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>西安小升初西安Java培训</title>
</head>
<body>
	<%
		Connection con = liuliking.getConn();
		Statement stmt = con.createStatement();
		String sql = "select * from people order by pk";
		ResultSet rs = stmt.executeQuery(sql);//实际上,每点一次链接,这个语句要执行一次
		String pages = request.getParameter("page");
		int MeiYeGeShu = 30;
		int pageInt = 1, i = 1;
		if (pages == null) {
		} else {
			pageInt = Integer.parseInt(pages);
		}
		while (i <= MeiYeGeShu * (pageInt - 1) && rs.next()) {//rs.next()起到了越过前面已经显示了的条目的作用
			i++;
		}
	%>
	<%
		int j = 1;
		while (rs.next() && j <= MeiYeGeShu) {//让游标继续后移,显示当前页面的20条记录
			j++;
			out.println(rs.getInt("pk"));
			out.println(rs.getString("Music"));
			out.println("<br/>");
		}
	%>
	<%
		int total = 0;//算总记录数

		sql = "select count(*) from people";
		rs = stmt.executeQuery(sql);
		while (rs.next())
			total = rs.getInt(1);
		out.println("总共有" + total + "条记录");
		out.println("<br/>");
		out.println("这是第" + pageInt + "页");
		out.println("<br/>");
	%>
	<%
		for (int jj = 1; jj < (total + 2 * MeiYeGeShu) / MeiYeGeShu; jj++) {
			//如果总共有100条记录,每页显示30条,不乘以2,只能有33页,显示到990条记录
	%>
	<a href="NewFile4.jsp?page=<%=jj%>">第<%=jj%>页
	</a>
	<%
		}
	%>
	<%
		try {
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception ex) {
		}
	%>
</body>
</html>
package net.liulixin.ku;

import java.sql.*;

public class LianJie {

	public LianJie() {
	}

	Connection conn = null;
	String re = "";
	// 设置你的数据库ip
	String dbip = "127.0.0.1";
	// 设置你的数据库用户名和密码:
	String use = "root";
	String pass = "HuaDi5";

	public java.sql.Connection getConn() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://" + dbip + ":3306/tutorial?user=" + use
					+ "&password=" + pass
					+ "&useUnicode=true&characterEncoding=gb2312";
			conn = DriverManager.getConnection(url);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.conn;
	}
}

源代码:http://pan.baidu.com/share/link?shareid=463381&uk=3878681452
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值