日期:2014-05-18  浏览次数:20707 次

oracle分页的问题,朋友帮忙看下
//调用的bean代码
package user;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SJYHDB {
  private Connection con;
  private Statement stm;
  private ResultSet rs;
  private String classname="oracle.jdbc.driver.OracleDriver";
  private String url="jdbc:oracle:thin:@此处隐去IP等信息";

  private int num_per=8;
  private int num_rs=0;
  private int pages_all=0;
  private int page_current=1;
  public SJYHDB(){}
  public Connection getCon(){  
  try{
  Class.forName(classname);
  }
  catch(ClassNotFoundException e){
  e.printStackTrace();
  }
  try{
  con=DriverManager.getConnection(url,"wj","123456");
  }
  catch(Exception e){
  e.printStackTrace(System.err);
  con=null;
  }
  return con;
  }
  public Statement getStm(){
  try{
  con=getCon();
  stm=con.createStatement();
  }catch(Exception e){e.printStackTrace(System.err);}
  return stm;
  }
  public Statement getStmed(){
  try{
  con=getCon();
  stm=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  }catch(Exception e){e.printStackTrace(System.err);}
  return stm;
  }
  //前台把页数传近来,计算总记录数和页数
  public void setPageInfo(int page){
  String sql="select id,username from account where s='1'";
try{
stm=getStmed();
rs=stm.executeQuery(sql);
rs.last(); //将记录指针移动到最后一条记录;
  this.num_rs=rs.getRow(); //获取当前指针所指记录的行号,即总记录数
this.pages_all=(num_rs%num_per==0)?(num_rs/num_per):(num_rs/num_per)+1; //计算出总页数;
if(page<1)
  this.page_current=1; //如果由参数传递过来的当前页数值小于1,则将当前页数设置为1;
else if(page>this.pages_all)
  this.page_current=this.pages_all; //如果由参数传递过来的当前页数值大于总页数,则将当前页数设置为总页

数;
else 
this.page_current=page;
}
catch(SQLException e){e.printStackTrace();}
}
  /**
  * @return
  */
  public ResultSet getPageRs(){
  int idnum=(this.page_current-1)*this.num_per;
  String sql="";
  //当页数小于1时执行
  if(this.page_current==1)
  sql="select id,username,realname,tel,email,address,to_char(recordtm,'yyyy-mm-dd hh24:mi:ss') as r 

from account where rownum<='"+this.num_per+"' and s='1' order by recordtm desc";
  //当页数大于1时执行,这边的SQL应该如何写才能实现每8条就分页??请朋友帮忙看看
else
sql="select id,username,realname,tel,email,address,recordtm,s from (select 

id,username,realname,tel,email,address,recordtm,rownum as rn,s from (select id,username,realname,tel,email,address,recordtm,s 

from account where s='1') a where rownum <= 8 and s='1') where rn >= 2 and s='1'";