JSP+MYSQL+Java類優(yōu)化分頁
發(fā)表時(shí)間:2024-05-21 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在JSP中經(jīng)常要用到查詢數(shù)據(jù)庫中的數(shù)據(jù),同常我們的做法是使用SQL語句“select * from tablename order by id desc”,這樣的做法有一個(gè)缺點(diǎn),當(dāng)數(shù)據(jù)庫很大的時(shí)候查詢的速度會(huì)變的很慢,在ASP中有一種方法 "select top "&r...
在JSP中經(jīng)常要用到查詢數(shù)據(jù)庫中的數(shù)據(jù),同常我們的做法是使用SQL語句“select * from tablename order by id desc”,這樣的做法有一個(gè)缺點(diǎn),當(dāng)數(shù)據(jù)庫很大的時(shí)候查詢的速度會(huì)變的很慢,在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為每頁顯示個(gè)數(shù), currentpage為當(dāng)前頁數(shù).不過在MYSQL數(shù)據(jù)庫中沒有“select top * " 語句,而可以代替的語句是”select * from tablename limit position, counter “position 指示從哪里開始查詢,如果是0則是從頭開始,counter 表示查詢的個(gè)數(shù),通過JSP+JAVA查詢數(shù)據(jù)庫,查詢獲取的數(shù)據(jù)暫時(shí)存放在內(nèi)存中在JSP中通過調(diào)取JAVA類,直接從內(nèi)存中提取數(shù)據(jù),速度有了很大提高。
下面的例子是一個(gè)關(guān)于網(wǎng)友評(píng)論的部分程序,假如你有一個(gè)專門供網(wǎng)友瀏覽的網(wǎng)站,而現(xiàn)在又想和網(wǎng)友互動(dòng)起來,加一個(gè)評(píng)論是不錯(cuò)的想法,那么你可以把下面的程序加上,建一個(gè)表其中加一個(gè)photo_id字段和你的表關(guān)聯(lián)起來后,就可以讓網(wǎng)友對(duì)你的圖片點(diǎn)評(píng)了。
Comment.java是一個(gè)評(píng)論的類
//<--------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)用請(qǐng)看下面的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");//從連接池中獲的一個(gè)連接
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條
%>
。糷tml>
。糷ead>
。紅itle>Untitled Document</title>
。糾eta 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("錯(cuò)誤!請(qǐng)確定你輸入的數(shù)字在1-"+hid+"之間");
return false;
}
window.location.href(gourl);
}</script>
。/head>
。糱ody>
<%
Comment[] p=null;
TestSql ts=null;
try
{
ts=new TestSql(con);
p=ts.getComment(intPageSize,CurrentPage);//ts=.getComments(PageSize(每頁顯示個(gè)數(shù)),Page(頁數(shù)))
intPageCount =ts.getPageCount(); //獲的頁數(shù)
intRowCount=p.length;
if(CurrentPage>intPageCount)
CurrentPage = intPageCount;
int total=ts.getTotal(); //獲取記錄總數(shù)
%>
。紅able width="748" border="0" align="center" cellpadding="0" cellspacing="0">
。紅r>
。紅d>
。紅able width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
。紅r>
<td height="17"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#EBEADF">
<tr>
。紅d height="25" bgcolor="#A7E081"><div align="center" class="style3">網(wǎng)友評(pí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://www.it.com.cn/f/edu/053/26/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
{
%>
。紅r>
<td height="20" bgcolor="#EBEADF">
。%
out.print(" 暫時(shí)沒有評(píng)論");
}
%>
</td>
。/tr>
<!-- end loop by tr -------------------------->
。/table></td>
</tr>
。紅r>
。紅d height="17" bgcolor="#FBFBF9">
<div align="center">
。糵orm style="margin:0 0 0 0 ">
。糳iv align="center">第<%=CurrentPage%>頁 共<%=intPageCount%>頁
<%if(CurrentPage>1){%>
。糰 href="comment.jsp?page=<%=CurrentPage-1%>">上一頁</a>
。%}else{%>
上一頁
。%}%>
<%if(CurrentPage>=intPageCount){%>
下一頁
。%}else{%>
。糰 href="comment.jsp?page=<%=CurrentPage+1%>">下一頁</a>
。%}%>
跳至
<input type="hidden" name="hid" value="<%=intPageCount%>">
。糹nput name="page" type="text" size="2" onChange="goto(this.form)">
頁
。糹nput 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)試通過