js| Program | Access sometimes counts the number of clicks for each article, if you want to update the library every time, 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 cache, and then reuse the database at intervals 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;
/**
*
* @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 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.*;
/**
*
* @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 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.*;
/**
*
* @author Http://www.tot.name
*/
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.