首页 > 编程 > JSP > 正文

JSP+MYSQL+Java类优化分页的实例

2024-09-05 00:20:42
字体:
来源:转载
供稿:网友

  在jsp中经常要用到查询数据库中的数据,同常我们的做法是使用sql语句“select * from tablename order by id desc”,这样的做法有一个缺点,当数据库很大的时候查询的速度会变的很慢,在asp中有一种方法 "select top "&recpage&" * from tablename where id not in (select top "&(recpage*(currentpage-1))&" id from products order by id desc) order by id desc"其中recpage为每页显示个数, currentpage为当前页数.不过在mysql数据库中没有“select top * " 语句,而可以代替的语句是”select * from tablename limit position, counter “position 指示从哪里开始查询,如果是0则是从头开始,counter 表示查询的个数,通过jsp+java查询数据库,查询获取的数据暂时存放在内存中在jsp中通过调取java类,直接从内存中提取数据,速度有了很大提高。

  下面的例子是一个关于网友评论的部分程序,假如你有一个专门供网友浏览的网站,而现在又想和网友互动起来,加一个评论是不错的想法,那么你可以把下面的程序加上,建一个表其中加一个photo_id字段和你的表关联起来后,就可以让网友对你的图片点评了。

  comment.java是一个评论的类

//<--------comment.java ------->
package dbconnection;
public class comment
{
 private string id;
 private string album_id;
 private string title;
 private string content;
 private string modi_time;
 private string user;
 public void setid(string ids)
 {
  this.id=ids;
 }
 public void setalbum_id(string album_ids)
 {
  this.album_id=album_ids;
 }
 public void settitle(string titles)
 {
  this.title=titles;
 }
 public void setcontent(string contents)
 {
  this.content=contents;
 }
 public void setmodi_time(string modi_times)
 {
  this.modi_time=modi_times;
 }
 public void setuser(string users)
 {
  this.user=users;
 }
 public string getid()
 {
  return id;
 }
 public string getalbum_id()
 {
  return album_id;
 }
 public string gettitle()
 {
  return title;
 }
 public string getcontent()
 {
  return content;
 }
 public string getmodi_time()
 {
  return modi_time;
 }
 public string getuser()
 {
  return user;
 }
}

  testsql.java就是我们查询数据库要用到的类了,具体的调用请看下面的comment.jsp文件。


|||/**
* title jsp+mysql优化分页的例子
* @author: cyd
* copyright: copyright (c) 2003
* @version 1.0
* 日期 2004-9-22
*/
//<--------testsql.java ------->
package dbconnection;
import java.sql.*;
import java.util.*;
public class testsql
{
 statement stmt=null;
 resultset rs=null;
 conn c=null;
 comment comments[]=null;
 vector v=null;
 int total;
 int pagesize;
 int pagecount;
 public testsql(connection cn) throws sqlexception
 {
  stmt=cn.createstatement();
 }
 //查询获取记录
 public comment[] getcomment(int pagesize,int page) throws sqlexception
 {
  this.pagesize=pagesize;
  string sql="select * from comment order by id desc limit "+(page-1)*pagesize+","+pagesize;
  comment comments[]=null;
  v=new vector();
  try
  {
   rs=stmt.executequery(sql);
   while(rs.next())
   {
    comment p=new comment();
    p.setid(rs.getstring("id"));
    p.settitle(rs.getstring("title"));
    p.setcontent(rs.getstring("content"));
    p.setmodi_time(rs.getstring("modi_time"));
    p.setuser(rs.getstring("user"));
    v.add(p);
   }
  }
  catch(sqlexception e)
  {
   system.err.println("err");
  }
  comments=new comment[v.size()];
  v.copyinto(comments);
  return comments;
 }
 //获取总记录数
 public int gettotal()
 {
  return total;
 }
 //获取总页数
 public int getpagecount()
 {
  try
  {
   rs=stmt.executequery("select count(*) from comment ");
   rs.next();
   this.total=rs.getint(1);
   this.pagecount=(rs.getint(1)+pagesize-1)/pagesize;
  }
  catch(sqlexception e)
  {
   system.err.println("err");
  }
  return pagecount;
 }
 //释放资源
 public void close() throws sqlexception
 {
  if (stmt != null)
  {
   stmt.close();
   stmt = null;
  }
  if (rs!=null)
  {
   rs.close();
   rs=null;
  }
 }
}
<!--comment.jsp -------------------------------------------------------------------->
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="dbconnection.dbconnectionmanager" %>
<%
 dbconnectionmanager connmgr;//这是数据库连接池的类,具体源码你可以在网找到。
 connmgr = dbconnectionmanager.getinstance();
 connection con = connmgr.getconnection("idb");//从连接池中获的一个连接
 int currentpage=1;
 int intpagecount,introwcount;
 if(request.getparameter("page")!=null)
  currentpage=integer.parseint(request.getparameter("page"));
 if(currentpage<1)
  currentpage=1;
  int intpagesize=5;//设置每页显示5条
%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.style3 {color: #ff0000}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #fffddf;
}
-->
</style>
<script language="javascript">
function goto(frm)
{
 var gourl ="comment.jsp?";
 gourl += "&page=" + (frm.page.value);
 var hid=parseint(frm.hid.value);
 if(parseint(frm.page.value)>hid||frm.page.value<=0){
  alert("错误!请确定你输入的数字在1-"+hid+"之间");
  return false;
 }
 window.location.href(gourl);
}</script>
</head>
<body>
<%
comment[] p=null;
testsql ts=null;
try
{
 ts=new testsql(con);
 p=ts.getcomment(intpagesize,currentpage);//ts=.getcomments(pagesize(每页显示个数),page(页数))
 intpagecount =ts.getpagecount(); //获的页数
 introwcount=p.length;
 if(currentpage>intpagecount)
  currentpage = intpagecount;
  int total=ts.gettotal(); //获取记录总数
 %>
 <table width="748" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="17"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ebeadf">
<tr>
<td height="25" bgcolor="#a7e081"><div align="center" class="style3">网友评论</div></td>
</tr>
<!-- start loop by tr -------------------------->
<%
if(introwcount>0)
{
 for(int i=0;i<introwcount;i++)
 {
  %>
  <tr>
  <td height="20">
  <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ebeadf">
  <tr>
   <td height="20">  <img src=http://tech.ddvip.com/2006-11/"image/dot11.gif" width="9" height="9"> <%=p[i].getuser()%>于 < %=p[i].getmodi_time()%> 留言 </td>
  </tr>
  <tr>
   <td bgcolor="#fbfbf9" style="padding:5px 5px 5px 5px;line-height:18px;"> <%=p[i].getcontent()%></td>
  </tr>
</table>
</td>
</tr>
<%
}
}
else
{
%>
<tr>
<td height="20" bgcolor="#ebeadf">
<%
out.print("   暂时没有评论");
}
%>
</td>
</tr>
<!-- end loop by tr -------------------------->
</table></td>
</tr>
<tr>
<td height="17" bgcolor="#fbfbf9">
<div align="center">
<form style="margin:0 0 0 0 ">
<div align="center">第<%=currentpage%>页  共<%=intpagecount%>页  
<%if(currentpage>1){%>
<a href="comment.jsp?page=<%=currentpage-1%>">上一页</a>  
<%}else{%>
上一页  
<%}%>
<%if(currentpage>=intpagecount){%>
下一页
<%}else{%>
<a href="comment.jsp?page=<%=currentpage+1%>">下一页</a>
<%}%>
跳至
<input type="hidden" name="hid" value="<%=intpagecount%>">
<input name="page" type="text" size="2" onchange="goto(this.form)">

<input type="button" name="button2" value="go->" style="font-size:12px ">
</div>
</form>
</div></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<%
}
catch(exception e)
{
 e.printstacktrace();
}
finally{
 connmgr.freeconnection("idb", con);
 connmgr.release();
 ts.close();
 p=null;
}
%>

  注:win2000+tomcat5.0调试通过;redhat9+tomcat5.0调试通过

注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表