点击这里给我发消息 点击这里给我发消息

jsp+java bean+mysql数据库进行分页显示

添加时间:2013-12-7
    相关阅读: 数据库 SQL

  bean文件:

  package bean;

  import java.sql.*;

  import java.util.Vector;

  public class Conn {

  public int resultPage;//用于取得最后是第几页的内容,因为给的要显示的页可能大于所有的页数,这时就要调整.

  public int totalPage;

  public Connection getConn()

  {

  Connection C=null;

  try {

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

  C= DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=admin&useUnicode=true&characterEncoding=8859_1");

  } catch (Exception e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  return C;

  }

  public void doDel(int ID)

  {

  Connection con=getConn();

  try {

  Statement st=con.createStatement();

  st.execute("delete from test where id="+ID);

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  public Vector getResult(int pageSize,int currentPage,String sql)

  {

  int rowNum=0;

  int page=0;

  int beginRow=0;

  Vector resultRows=new Vector();

  try{

  Statement st=getConn().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

  ResultSet rs=st.executeQuery(sql);

  rs.last();

  rowNum=rs.getRow();

  if(rowNum%pageSize==0)

  {

  page=rowNum/pageSize;

  }

  else

  page=rowNum/pageSize+1;

  if(currentPage>page)

  {

  currentPage=page;

  }

  if(currentPage<1)

  {

  currentPage=1;

  }

  resultPage=currentPage;

  totalPage=page;

  beginRow=(currentPage-1)*pageSize+1;

  //System.out.println("beginRow:"+beginRow);

  rs.absolute(beginRow);

  int i=1;

  while(!rs.isAfterLast() && i<=pageSize)

  {

  Vector v=new Vector();

  v.add(rs.getInt(1));

  v.add(rs.getString(2));

  v.add(rs.getInt(3));

  resultRows.add(v);

  i++;

  rs.next();

  }

[1] [2] 下一页

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册