⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stepthree.jsp

📁 一个简单的分页显示java源码
💻 JSP
字号:
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="conn" scope="page" class="db.ConnOracle"/>

<%!
ResultSet rs = null;
ResultSet rsTmp = null;
String sql = "";
int PageSize = 4;
int Page = 2;
int totalPage = 0;
String str = "";

public String ShowOnePage(ResultSet rs, int Page, int PageSize) {
	  str = "";
	  // 先将记录指针定位到相应的位置
	  try {
	  		rs.absolute( (Page-1) * PageSize + 1);
	  }catch(SQLException e) {
	  }
	        
	  for(int i=1; i<=PageSize; i++) {
	     str += RsToGbook(rs);
		 try {
		     if(!rs.next()) break;
		 }catch(Exception e) {
			e.printStackTrace();
		 }
	  }
	  return str;
}
	
// 显示单行记录子模块
public String RsToGbook( ResultSet rs ) {
		String text= "";
		try {
	  		text += "<tr>";
		    text += "<td>" + rs.getString("stockid") + "</td>";
		    text += "<td>" + rs.getString("stockname") + "</td>";
		    text += "<td>" + rs.getFloat("shoupan") + "</td>";
		    text += "<td>" + rs.getFloat("zhangdie") + "</td>";
		    text += "<td>" + rs.getFloat("stockmax") + "</td>";
		    text += "<td>" + rs.getFloat("stockmin") + "</td>";
		    text += "<td>" + rs.getFloat("stocknum") + "</td>";
	        text += "</tr>";
		}catch(Exception e) {
			e.printStackTrace();
		}
		return text;
}
%>

<%
   sql = "select * from stock";
   try {
       rs = conn.query( sql );
   }catch(Exception e) {
       out.println("访问数据库出错!");
   }
%>
<html>

<head>
	<title>分页浏览</title>
</head>

<body bgcolor="#FFFFFF">

<h2 align="center">分页控制第三步</h2>

<hr>
<center>
<table border> 
	<tr bgcolor=lightblue>
		<th>stockid</th>
		<th>stockname</th>
		<th>shoupan</th>
		<th>zhangdie</th>
		<th>stockmax</th>
		<th>stockmin</th>
		<th>stocknum</th>
	</tr>
<%
	rsTmp = conn.query("select count(*) as mycount from stock");
	rsTmp.next();
	int totalrecord = rsTmp.getInt("mycount");
	if(totalrecord % PageSize ==0) totalPage = totalrecord / PageSize;  // 如果是当前页码的整数倍
	else  totalPage = (int) Math.floor( totalrecord / PageSize ) + 1;   // 如果最后还空余一页

	if(totalPage == 0) totalPage = 1;
	rsTmp.close();

	try {
		if(request.getParameter("Page")==null || request.getParameter("Page").equals("")) 
		   Page = 1;
		else
		   Page = Integer.parseInt(request.getParameter("Page"));
	} catch(java.lang.NumberFormatException e) {  // 捕获用户从浏览器地址拦直接输入Page=sdfsdfsdf所造成的异常
		   Page = 1;
	}

	if(Page < 1)  Page = 1;
	if(Page > totalPage) Page = totalPage;

	out.println(ShowOnePage(rs, Page, PageSize));
%>
</table>
<form Action="stepThree.jsp" method="get">
<% 
   if(Page != 1) {
      out.println("<a href= stepThree.jsp?Page=1>第一页</A>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -