只对新用户计数

本文介绍了一个专门针对新用户的网站访问计数系统的实现方法,该系统通过记录每个唯一IP地址的访问次数来统计新用户数量,并使用Java与SQL Server数据库进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只对新用户计数
表结构 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>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值