您的位置: 首页 >> 新闻中心 >> 计算机 >> 软件开发
JSP高访问量下的计数程序
■ 最新课程推荐更多课程>>
学校培训课程开课时间上课地点精英价报名
正辰培训 微软软件测试工程师电话预约西直门教学区¥4704
新 科 海 软件测试工程师就业班电话预约海淀长远天地¥6280
北师大IT 软件工程与测试实战班电话预约北京师范大学¥1800
北师大IT 高级网络工程师就业班电话预约北京师范大学¥13000
金 同 方 网络工程师就业周末班电话预约人大总部¥7000
  有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。源码如下:

  CountBean.java

/*
* CountData.java
*
* Created on 2006年10月18日, 下午4:44
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

  package com.tot.count;

/**
*
* @author http://www.tot.name
*/
public class CountBean {
 private String countType;
 int countId;
 /** Creates a new instance of CountData */
 public CountBean() {}
 public void setCountType(String countTypes){
  this.countType=countTypes;
 }
 public void setCountId(int countIds){
  this.countId=countIds;
 }
 public String getCountType(){
  return countType;
 }
 public int getCountId(){
  return countId;
 }
}

  CountCache.java

/*
* CountCache.java
*
* Created on 2006年10月18日, 下午5:01
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

package com.tot.count;
import java.util.*;
/**
*
* @author http://www.tot.name
*/
public class CountCache {
 public static LinkedList list=new LinkedList();
 /** Creates a new instance of CountCache */
 public CountCache() {}
 public static void add(CountBean cb){
  if(cb!=null){
   list.add(cb);
  }
 }
}

 CountControl.java

 /*
 * CountThread.java
 *
 * Created on 2006年10月18日, 下午4:57
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.tot.count;
import tot.db.DBUtils;
import java.sql.*;
/**
*
* @author http://www.tot.name
*/
public class CountControl{
 private static long lastExecuteTime=0;//上次更新时间 
 private static long executeSep=60000;//定义更新间隔时间,单位毫秒
 /** Creates a new instance of CountThread */
 public CountControl() {}
 public synchronized void executeUpdate(){
  Connection conn=null;
  PreparedStatement ps=null;
  try{
   conn = DBUtils.getConnection();
   conn.setAutoCommit(false);
   ps=conn.prepareStatement("update t_news set hits=hits+1 where id=?");
   for(int i=0;i<CountCache.list.size();i++){
    CountBean cb=(CountBean)CountCache.list.getFirst();
    CountCache.list.removeFirst();
    ps.setInt(1, cb.getCountId());
    ps.executeUpdate();⑴
    //ps.addBatch();⑵
   }
   //int [] counts = ps.executeBatch();⑶
   conn.commit();
  }catch(Exception e){
   e.printStackTrace();
  } finally{
  try{
   if(ps!=null) {
    ps.clearParameters();
ps.close();
ps=null;
  }
 }catch(SQLException e){}
 DBUtils.closeConnection(conn);
 }
}
public long getLast(){
 return lastExecuteTime;
}
public void run(){
 long now = System.currentTimeMillis();
 if ((now - lastExecuteTime) > executeSep) {
  //System.out.print("lastExecuteTime:"+lastExecuteTime);
  //System.out.print(" now:"+now+"\n");
  // System.out.print(" sep="+(now - lastExecuteTime)+"\n");
  lastExecuteTime=now;
  executeUpdate();
 }
 else{
  //System.out.print("wait for "+(now - lastExecuteTime)+" seconds:"+"\n");
 }
}
}
//注:如果你的数据库驱动支持批处理,那么可以将⑵,⑶标记的代码前的注释去掉,同时在代码⑴前加上注释

  类写好了,下面是在JSP中如下调用。

本新闻共2页,当前在第1页  1  2

  影视动画培训   北大BEC培训官方报名网站   2008美国夏令营启航官方指定报名网站   2008留学第一站!  
  北师大 火星时代
共举影视动画培训之鼎
  北大BEC培训官方报名网站
现在报名独享95折!
  2008年国家职业资格考试
一次过关完全备考手册
  2008留学第一站
留学资讯尽在精英留学站!
 
上一篇:Tomcat JSP经典配置实例
下一篇:JSP和JSF双剑合并 打造完美Web应用
 相关新闻
·内存调试技巧:C 语言最大难点揭秘·C程序实现汉字内码与GB码
·关于C#静态构造函数的几点说明·使用c#捕获windows的关机事件
·Java版本和C++版本简单Stack程序·解释一个关于Hibernate lazy的问题
·专家详解:复杂表达式的执行步骤·教你如何在C语言使用位运算实现循环移位
·关于C#代码实现ControlTemplate·ASP.NET中为GridView添加删除提示框
·ASP.NET中为GridView添加删除提示框·实例:用Visual C#制作新闻阅读器
·《C++0x漫谈》系列之:右值引用·缓冲区溢出原理浅析以及防护
·C++ builder 的文件读写操作总结·JSP和JSF双剑合并 打造完美Web应用
 
◇ 重点栏目导航
◇ 精英服务承诺
教育顾问:010-51660910
QQ交流:138660910
相关资料
·软件测试新手的修炼之路
·Smarty简体中文参考手册
·Struts中文手册
·Struts快速学习指南
·ultradev动态网页制作教程
·UML工具箱
·《设计模式》中文版
·学友Flash伴侣 1.11
·阿须图像水印(AssureMark)V2.0
·超级语霸
相关试题
·2008年云南公务员考试专业试卷之科技环保
·2008年云南公务员考试试卷之教育文化类专
·2008年云南公务员考试试卷参考答案之科技
·2008年云南公务员考试试卷参考答案之教育
·2008年公务员考试科教管理类专业试卷参考
·2008年公务员考试科教管理类专业试卷(云
·2007年全国CPA考试试卷及答案解析之《会
·2007年CPA试卷及答案解析之《财务成本管
·2008年注会考前模拟试题之《财务成本管理
·2007年全国CPA《税法》考试试卷及答案解
相关热贴
·如何改QQ IP地址!
·恰当选择软件测试自动化方案
·ADO.NET学习总结
·.net操纵xml文件类(c#)
·Log4net教程
·VPN技术详解
·高手必读 网络端口安全防护技巧放送
·访问XP共享出现的问题解决办法
·Web2.0时代,RSS你会用了吗?(技术实现总
·.NET下正则表达式应用的四个示例