六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

JSP+MYSQL+Java類優化分頁

[摘要]在JSP中經常要用到查詢數據庫中的數據,同常我們的做法是使用SQL語句“select * from tablename order by id desc”,這樣的做法有一個缺點,當數據庫很大的時候查詢的速度會變的很慢,在ASP中有一種方法 "select top "&r...
在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://www.it.com.cn/f/edu/053/26/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調試通過


主站蜘蛛池模板: 天天插天天色 | 四虎永久免费在线观看 | 三区在线视频 | 日本高清色www | 色色视频网 | 日日噜噜夜夜躁躁狠狠 | 永久免费观看午夜视频在线 | 欧美一级淫片免费播放口 | 欧美一区二区三区播放 | 欧美性xxxx | 日本在线免费观看视频 | 亚洲高清免费观看 | 婷婷六月色| 天堂√中文在线 | 日韩一区二紧身裤 | 日本a网站| 青青青青青在线视频播放 | 手机看片自拍日韩日韩高清 | 青娱乐免费视频在线观看 | 欧美午夜a级限制福利片 | 三级国产 | 亚洲成a人v在线观看 | 伊人精品在线观看 | 伊人成人在线观看 | 日韩在线精品视频 | 青青草原1769久久免费播放 | 青青草a | 亚洲福利精品一区二区三区 | 日韩成人在线免费视频 | 天天综合射 | 日韩精品亚洲专区在线观看 | 亚洲免费高清视频 | 青青青国产在线观看免费网站 | 手机亚洲第1页 | 亚洲视频一二区 | 青青青国产在线观看 | 全免费a级毛片免费看不卡 全黄一级片 | 青春草视频下载 | 欧美视频日韩视频 | 日日奸 | 欧美在线免费播放 |