中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>程序设计>Java>列表

jsp的分页查询代码(mysql数据库)

来源:www.javadby.com 作者: 时间:2007-12-29 点击:

  这个功能一共创建了两个javabean组件和一个jsp页面显示分页页面,第一个是处理以数据库连接的javabean,第一个javabean是处理分页查询结果的代码,第三个jsp是调用第二个javabean,显示分页查询结果!

  //下面是连接mysql数据库的一个javabean的代码:

  package data;

  import java.sql.*;

  public class LoginData{

  Connection conn=null;

  public LoginData(){

  this.connect();

  }

  

  public Connection getConn(){

  return this.conn;

  }

  public boolean connect(){

  try{

  //使用JDBC桥创建数据库连接

  Class.forName("org.gjt.mm.mysql.Driver").newInstance();

  

  //使用DriverManager类的getConnection()方法建立连接

  //第一个参数定义用户名,第二个参数定义密码

  this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312","root","123456");

  }catch(Exception ex){

  ex.printStackTrace();

  return false;

  }

  return true;

  }

  }

  //下面是一个javabean的处理mysql数据库的分页显示查询的代码

  package data;

  import java.sql.*;

  import java.util.*;

  public class strongSplitPage

  {

  private Connection conn=null;

  private Statement stmt=null;

  private ResultSet rs=null;

  private ResultSetMetaData rsmd=null;

  //sql 查询语句

  private String sqlStr;

  //总纪录数目

  private int rowCount;

  //所分得逻辑页数

  private int pageCount;

  //每页显示的纪录数目

  private int pageSize;

  //定义表的列数目

  private int columnCount;

  private int irows;

  public void initialize(String sqlStr,int pageSize,int showPage)

  {

  this.sqlStr=sqlStr;

  this.irows=pageSize*(showPage-1);

  this.pageSize=pageSize;

  try

  {

  LoginData loginData=new data.LoginData();

  this.conn=loginData.getConn();

  this.stmt=this.conn.createStatement();

  this.rs=this.stmt.executeQuery(this.sqlStr);

  this.rsmd=this.rs.getMetaData();

  if(this.rs!=null)

  {

  this.rs.last();

  this.rowCount=this.rs.getRow();

  this.rs.first();

  this.columnCount=this.rsmd.getColumnCount();

  this.pageCount=(this.rowCount-1)/this.pageSize+1;

  this.rs.close();

  this.stmt.close();

  }

  this.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;

  this.stmt=this.conn.createStatement();

  this.rs=this.stmt.executeQuery(this.sqlStr);

  }catch(Exception ex)

  {

  ex.printStackTrace();

  }

  }

  public Vector getPage()

  {

  Vector vData=new Vector();

  try

  {

  if(this.rs!=null)

  {

  

  while(this.rs.next())

  {

  String[] sData=new String[this.columnCount];

  for(int j=0;j

  {

  sData[j]=this.rs.getString(j+1);

  }

  vData.addElement(sData);

  }

  this.rs.close();

  this.stmt.close();

  this.conn.close();

  }

  }catch(Exception ex)

  {

  ex.printStackTrace();

  }

  return vData;

  }

  

  //获得页面总数

  public int getPageCount()

  {

  return this.pageCount;

  }

  //获得数据表中总纪录数

  public int getRowCount()

  {

  return this.rowCount;

  }

  }

  //下面是jsp显示分页页面

  

  

  

  

  

  

    //显示每页的纪录数

  int pageSize=10;

  String sqlStr="";

  //当前页

  int showPage=1;

  %>

  

  

  

  

  

  

  

  

个人基本信息

 

  

 

  

 

 

 

 

 

 

 

编号 学号 姓名

  

 

  

  

条 条/页  第页/共页   [首页

  

  [上一页

    }

  else{

  %>

  [上一页] 

  

  [下一页

    }

  else{

  %>

  [下一页] 

    }

  %>

  

  [尾页] 转到

  

  页   

  

  

 

  

  

 

  

 

  

  

  

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册