日期:2014-05-16  浏览次数:20387 次

java_JDBC---------分页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" import="com.db.*" import="javax.servlet.http.HttpSession;" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
		HttpSession session1 = request.getSession(); 
		String affairsfzid=(String)session1.getAttribute("affairsfzid");	
		Jdbc db=new Jdbc();
		Jdbc db1=new Jdbc();
		String sql="select * from tb_affair where GscPubUserIdCardNum  like '%"+affairsfzid+"%'";//GscProjectCodeId='"+affairsfzid+"'
		String sql1 = "select * from tb_affair where GscPubUserIdCardNum  like '%"+affairsfzid+"%'";//计算条数的sql
		
		ResultSet rs = null;
		ResultSet rs1 = null;
		
		rs = db.executeQuery(sql);
  		rs1 = db1.executeQuery(sql1);
  		
  		
  		//下边是分页的一些处理
  		int pageSize = 20;//每页显示条数
  		int rowCount = 0;
  		while(rs1.next())
  		rowCount = rs1.getInt(1	);
  		int pageCount;
  		int curPage;
  		String strPage = request.getParameter("page");
  		curPage  = strPage == null ?  1 : Integer.parseInt(strPage) ;
  		curPage = curPage <1 ? 1 :curPage;
  		pageCount = (rowCount + pageSize -1)/pageSize;
  		curPage = curPage > pageCount ? pageCount : curPage;
  		int thePage = (curPage - 1) * pageSize;
  		int n = 0;
  		rs.absolute(thePage + 1	); 
		
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>JDBC查询分页.赵永恩</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<LINK href="./images/nes_style.css" type="text/css" rel="stylesheet"></LINK>
  </head>
<body>
[align=center"  style="font-size:12px;]
<!-- 这地方是下边调用显示的地方 -->
第<%=curPage%>页 共<%=pageCount%>页 共<%=rowCount%>条 
<%if(curPage>1){%>[url=affairrequest.jsp]首页[/url]<%}%> 
<%if(curPage>1){%>[url=affairrequest.jsp?page=<%=curPage-1%>]上一页[/url]<%}%> 
<%if(curPage<pageCount){%>[url=affairrequest.jsp?page=<%=curPage+1%>]下一页[/url]<%}%> 
<%if(pageCount>1){%>[url=affairrequest.jsp?page=<%=pageCount%>]尾页[/url]<%}%> 
[url=#]<input type="text" size="10" name="num">GO[/url]<!-- 这个正在考虑中 -->
					  	</td>
					  </tr>
			</table>
		[/align]		
	</div>

</div>
</body>
</html>



package com.db; 

import java.sql.*; 

/**
 * 
 * 连接sql server2000
 * @author 赵永恩
 *
 */
public class Jdbc{ 
	
	String dbUrl="jdbc:sqlserver://localhost:1433; DatabaseName=SczwWeb"; 
	String theUser="sa"; 
	String thePw=""; 
	Connection c=null; 
	Statement conn; 
	ResultSet rs=null; 
	
public Jdbc(){ 
	try{ 
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
		c = DriverManager.getConnection(dbUrl,theUser,thePw); 
		//创建滚动结果集 需要把创建stme对象后多加两个参数ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
		conn=c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
		
	}catch(Exception e){ 
		e.printStackTrace(); 
	} 
} 

/**
 * 更新
 * @param sql
 * @return
 */
public boolean executeUpdate(String sql){ 
	try { 
		conn.executeUpdate(sql); 
		return true; 
	} catch (SQLException e) { 
		e.printStackTrace(); 
		return false; 
	} 
} 




/**
 * 查询
 * @param sql
 * @return
 */
public ResultSet executeQuery(String sql) { 
rs=null; 
	try { 
		rs=conn.executeQuery(sql); 
	} catch (SQLException e) { 
		e.printStackTrace();