国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP+MYSQL+Java類優(yōu)化分頁的實例

2024-09-05 00:20:42
字體:
供稿:網(wǎng)友

  在jsp中經(jīng)常要用到查詢數(shù)據(jù)庫中的數(shù)據(jù),同常我們的做法是使用sql語句“select * from tablename order by id desc”,這樣的做法有一個缺點,當數(shù)據(jù)庫很大的時候查詢的速度會變的很慢,在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為每頁顯示個數(shù), currentpage為當前頁數(shù).不過在mysql數(shù)據(jù)庫中沒有“select top * " 語句,而可以代替的語句是”select * from tablename limit position, counter “position 指示從哪里開始查詢,如果是0則是從頭開始,counter 表示查詢的個數(shù),通過jsp+java查詢數(shù)據(jù)庫,查詢獲取的數(shù)據(jù)暫時存放在內(nèi)存中在jsp中通過調(diào)取java類,直接從內(nèi)存中提取數(shù)據(jù),速度有了很大提高。

  下面的例子是一個關(guān)于網(wǎng)友評論的部分程序,假如你有一個專門供網(wǎng)友瀏覽的網(wǎng)站,而現(xiàn)在又想和網(wǎng)友互動起來,加一個評論是不錯的想法,那么你可以把下面的程序加上,建一個表其中加一個photo_id字段和你的表關(guān)聯(lián)起來后,就可以讓網(wǎng)友對你的圖片點評了。

  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就是我們查詢數(shù)據(jù)庫要用到的類了,具體的調(diào)用請看下面的comment.jsp文件。


|||/**
* title jsp+mysql優(yōu)化分頁的例子
* @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;
 }
 //獲取總記錄數(shù)
 public int gettotal()
 {
  return total;
 }
 //獲取總頁數(shù)
 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;//這是數(shù)據(jù)庫連接池的類,具體源碼你可以在網(wǎng)找到。
 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;//設(shè)置每頁顯示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("錯誤!請確定你輸入的數(shù)字在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(每頁顯示個數(shù)),page(頁數(shù)))
 intpagecount =ts.getpagecount(); //獲的頁數(shù)
 introwcount=p.length;
 if(currentpage>intpagecount)
  currentpage = intpagecount;
  int total=ts.gettotal(); //獲取記錄總數(shù)
 %>
 <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">網(wǎng)友評論</div></td>
</tr>
<!-- start loop by tr -------------------------->
<%
if(introwcount>0)
{
 for(int i=0;i<introwcount;i++)
 {
  %>
 ?。紅r>
 ?。紅d height="20">
 ?。紅able width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ebeadf">
  <tr>
  ?。紅d 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>
 ?。紅r>
   <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調(diào)試通過;redhat9+tomcat5.0調(diào)試通過

注冊會員,創(chuàng)建你的web開發(fā)資料庫,
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴城市| 惠水县| 杂多县| 澜沧| 游戏| 察隅县| 互助| 北宁市| 钟祥市| 宁陵县| 澄迈县| 清水县| 家居| 连云港市| 扎鲁特旗| 资阳市| 南漳县| 吉水县| 耒阳市| 铜山县| 双江| 寿宁县| 叶城县| 广安市| 铁力市| 鄱阳县| 阳春市| 永登县| 历史| 昌黎县| 瑞安市| 浦县| 阜宁县| 永年县| 泽普县| 乾安县| 汨罗市| 咸阳市| 饶阳县| 东平县| 长葛市|