Count new users only
Table Structure tb_newusercount field user_id, user_ip, user_times
DB type
Package com. Count. online;
Import java. SQL .*;
Public class dB {
Private connection con;
Private statement STM;
Private resultset RS;
Private string classname = "com. Microsoft. JDBC. sqlserver. sqlserverdriver ";
Private string
Url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = db_database08 ";
Public dB (){}
Public connection getcon (){
Try {
Class. forname (classname );
}
Catch (classnotfoundexception e ){
E. printstacktrace ();
}
Try {
Con = drivermanager. getconnection (URL, "sa ","");
}
Catch (exception e ){
E. printstacktrace (system. Err );
Con = NULL;
}
Return con;
}
Public Statement getstm (){
Try {
Con = getcon ();
STM = con. createstatement ();
} Catch (exception e) {e. printstacktrace (system. Err );}
Return STM;
}
Public Statement getstmed (){
Try {
Con = getcon ();
STM = con. createstatement
(Resultset. type_scroll_insensitive, resultset. concur_read_only );
} Catch (exception e) {e. printstacktrace (system. Err );}
Return STM;
}
Public resultset search (string SQL ){
Getstmed ();
Try {
Rs1_cmd.exe cutequery (SQL );
} Catch (exception e) {e. printstacktrace ();}
Return Rs;
}
Public int dosql (string SQL ){
Int I =-1;
Getstmed ();
Try {
I =cmd.exe cuteupdate (SQL );
} Catch (exception e) {e. printstacktrace ();}
Return I;
}
Public void closed (){
Try {
If (RS! = NULL) Rs. Close ();
}
Catch (exception e) {e. printstacktrace ();}
Try {
If (STM! = NULL) STM. Close ();
}
Catch (exception e) {e. printstacktrace ();}
Try {
If (con! = NULL) con. Close ();
}
Catch (exception e) {e. printstacktrace ();}
}
}
Countonline. Java class
Package com. Count. online;
Import java. SQL .*;
Public class countonline {
Private string userip;
Private string nowdate;
Private int times;
Private dB DB = new dB ();
Public countonline (){}
Public void setuserip (string userip ){
This. userip = userip;
}
Public String getuserip (){
Return this. userip;
}
Public void setnowdate (string nowdate ){
This. nowdate = nowdate;
}
Public String getnowdate (){
Return this. nowdate;
}
Public void settimes (INT times ){
This. Times = times;
}
Public int gettimes (){
Return this. times;
}
Public resultset checkuser (){
String SQL = "select * From tb_newusercount where
User_ip = '"+ this. userip + "'";
Resultset rs = NULL;
Try {
Rs = dB. Search (SQL );
If (Rs. Next ()){
This. Times = Rs. getint ("user_times") + 1;
SQL = "Update tb_newusercount set user_times =" + this. Times + "where
User_ip = '"+ this. userip + "'";
DB. dosql (SQL );
}
Else {
This. Times = 1;
SQL = "insert into tb_newusercount values ('" + this. userip + "', 1 )";
DB. dosql (SQL );
}
Rs = dB. Search ("select * From tb_newusercount ");
} Catch (exception e) {e. printstacktrace ();}
Return Rs;
}
Public void dbclose (){
DB. Closed ();
}
}
Index. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<JSP: usebean id = "mycount" class = "com. Count. Online. countonline"/>
<%
String IP = request. getremoteaddr ();
Mycount. setuserip (IP );
Resultset rs = mycount. checkuser ();
Rs. Last ();
Int num = Rs. getrow ();
%>
<HTML>
<Head>
<Title> only counters for new users </title>
<LINK rel = "stylesheet" type = "text/CSS" href = "CSS/style.css">
</Head>
<Body>
<Center>
<Table Height = "90" width = "200" border = "1" bordercolor = "black"
Bordercolorlight = "black" bordercolordark = "white" cellspacing = "0" style = "margin-
Top: 200 ">
<Tr bgcolor = "lightgrey">
<TD align = "center"> visitor IP address </TD>
<TD align = "center"> Number of visits </TD>
</Tr>
<%
Rs. beforefirst ();
While (Rs. Next ()){
%>
<Tr>
<TD align = "center"> <% = Rs. getstring ("user_ip") %> </TD>
<TD align = "center"> <% = Rs. getint ("user_times") %> </TD>
</Tr>
<%
}
%>
<Tr>
<TD align = "center" colspan = "2">
Your IP address is: <% = IP %>
<Br>
Your access times: <% = mycount. gettimes () %>
<Br>
A total of <% = num %> new users have accessed this page (IP addresses are used to calculate how many users have accessed this page)
</TD>
</Tr>
</Table>
<%
Mycount. dbclose ();
%>
</Center>
</Body>
</Html>