Count new users only

Source: Internet
Author: User

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>
 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.