首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

oracle分页的有关问题,朋友帮忙看下

2012-01-08 
oracle分页的问题,朋友帮忙看下//调用的bean代码package userimport java.sql.Connectionimport java.sq

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'";
//数据表中字段s为标志位
System.out.println("sql:::"+sql);
  try{
  rs=stm.executeQuery(sql);
  }
  catch(Exception e){e.printStackTrace();}
  return rs;
  }
  public int getNumper(){
  return this.num_per;
  }
  public int getNumrs(){
  return this.num_rs;
  }


  public int getPagesall(){
  return this.pages_all;
  }
  public int getPagecurrent(){
  return this.page_current;
  }
  public void closed(){
  try{
  if(rs!=null)rs.close();
  }
  catch(Exception e){e.printStackTrace();}
  try{
  if(stm!=null)stm.close();
  }
  catch(Exception e){e.printStackTrace();}
  try{
  if(con!=null)con.close();
  }
  catch(Exception e){e.printStackTrace();}
  }
}
请朋友结合我的代码给我改下,我试了很久了,老是报错
多谢了。

[解决办法]
String sql_org = "select id,username,realname,tel,email,address,recordtm where s='1'";
String sql = 
"SELECT * FROM (select temp.*, ROWNUM rn FROM (" 
+ sql_org 
+ ") temp where ROWNUM <= " + idnum
+ ") where rn >" + (idnum - 8);
[解决办法]

探讨
String sql_org = "select id,username,realname,tel,email,address,recordtm where s='1'";
String sql =
"SELECT * FROM (select temp.*, ROWNUM rn FROM ("
+ sql_org
+ ") temp where ROWNUM <= " + idnum
+ ") where rn >" + (idnum - 8);

热点排行