有时要为每一篇文章统计其点击次数如果每一次浏览都要更新一次库的话那性能在访问量很大的情况下服务器的压力就会很大了比较好一点的方法就是先将要更新的数据缓存起来然后每隔一段时间再利用数据库的批量处理批量更新库源码如下
CountBeanjava
/*
* CountDatajava
*
* Created on 年月日 下午:
*
* To change this template choose Tools | Options and locate the template under
* the Source Creation and Management node Rightclick the template and choose
* Open You can then make changes to the template in the Source Editor
*/
package comtotcount;
/**
*
* @author
*/
public class CountBean {
private String countType;
int countId;
/** Creates a new instance of CountData */
public CountBean() {}
public void setCountType(String countTypes){
thiscountType=countTypes;
}
public void setCountId(int countIds){
thiscountId=countIds;
}
public String getCountType(){
return countType;
}
public int getCountId(){
return countId;
}
}
CountCachejava
/*
* CountCachejava
*
* Created on 年月日 下午:
*
* To change this template choose Tools | Options and locate the template under
* the Source Creation and Management node Rightclick the template and choose
* Open You can then make changes to the template in the Source Editor
*/
package comtotcount;
import javautil*;
/**
*
* @author
*/
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){
listadd(cb);
}
}
}
CountControljava
/*
* CountThreadjava
*
* Created on 年月日 下午:
*
* To change this template choose Tools | Options and locate the template under
* the Source Creation and Management node Rightclick the template and choose
* Open You can then make changes to the template in the Source Editor
*/
package comtotcount;
import totdbDBUtils;
import javasql*;
/**
*
* @author
*/
public class CountControl{
private static long lastExecuteTime=;//上次更新时间
private static long executeSep=;//定义更新间隔时间单位毫秒
/** Creates a new instance of CountThread */
public CountControl() {}
public synchronized void executeUpdate(){
Connection conn=null;
PreparedStatement ps=null;
try{
conn = DBUtilsgetConnection();
connsetAutoCommit(false);
ps=connprepareStatement("update t_news set hits=hits+ where id=?");
for(int i=;i<CountCachelistsize();i++){
CountBean cb=(CountBean)CountCachelistgetFirst();
CountCachelistremoveFirst();
pssetInt( cbgetCountId());
psexecuteUpdate();⑴
//psaddBatch();⑵
}
//int [] counts = psexecuteBatch();⑶
conncommit();
}catch(Exception e){
eprintStackTrace();
} finally{
try{
if(ps!=null) {
psclearParameters();
psclose();
ps=null;
}
}catch(SQLException e){}
DBUtilscloseConnection(conn);
}
}
public long getLast(){
return lastExecuteTime;
}
public void run(){
long now = SystemcurrentTimeMillis();
if ((now lastExecuteTime) > executeSep) {
//Systemoutprint("lastExecuteTime:"+lastExecuteTime);
//Systemoutprint(" now:"+now+"n");
// Systemoutprint(" sep="+(now lastExecuteTime)+"n");
lastExecuteTime=now;
executeUpdate();
}
else{
//Systemoutprint("wait for "+(now lastExecuteTime)+" seconds:"+"n");
}
}
}
//注如果你的数据库驱动支持批处理那么可以将⑵⑶标记的代码前的注释去掉同时在代码⑴前加上注释
类写好了下面是在JSP中如下调用
<%
CountBean cb=new CountBean();
cbsetCountId(IntegerparseInt(requestgetParameter("cid")));
CountCacheadd(cb);
outprint(CountCachelistsize()+"<br>");
CountControl c=new CountControl();
crun();
outprint(CountCachelistsize()+"<br>");
%>