js| Program | access
Sometimes you have to count the number of clicks for each article, and if you want to update the library every time you browse, that performance in the traffic is very large, the pressure on the server will be very large, a better way is to update the data first cached, and then use the database for a period of batch processing, batch update library. The source code is as follows:
Countbean.java
/*
* Countdata.java
*
* Created on October 18, 2006, 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;
/**
*
*/
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 October 18, 2006, 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.*;
/**
*
*/
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 October 18, 2006, 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.*;
/**
*/
public class countcontrol{
Private static long lastexecutetime=0;//last update time
Private static long executesep=60000;//defines the update interval, in milliseconds
/** 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");
}
}
}
Note: If your database driver supports batch processing, you can remove the comments in front of the ⑵,⑶ tagged code and annotate the code before ⑴
Class is written, the following is called in the JSP below.
<%
Countbean cb=new Countbean ();
Cb.setcountid (Integer.parseint (Request.getparameter ("CID"));
Countcache.add (CB);
Out.print (CountCache.list.size () + "
");
Countcontrol c=new Countcontrol ();
C.run ();
Out.print (CountCache.list.size () + "
");
%>