只对新用户计数
表结构 tb_newusercount 字段user_id,user_ip,user_times
DB类
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{
rs=stm.executeQuery(sql);
}catch(Exception e){e.printStackTrace();}
return rs;
}
public int dosql(String sql){
int i=-1;
getStmed();
try{
i=stm.executeUpdate(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类
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>只对新用户计数的计数器</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">访问者IP地址</td>
<td align="center">访问次数</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">
您的IP为:<%=ip%>
<br>
您的访问次数为:<%=mycount.getTimes()%>次
<br>
共有 <%=num%> 个新用户访问过本页(以ip计算有多少个用户访问过)
</td>
</tr>
</table>
<%
mycount.dbclose();
%>
</center>
</body>
</html>