package com.chen;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VisitorCounter {
	private final static String TABLE_NAME = "demo";
	private static String today = null;
	private static long today_num = 0;
	private static long total_num = 0;

	// 加载访问量
	public static void loadNum() {
		if (total_num < 1)
			loadTotalNum();
		if (today_num < 1)
			loadToadyNum();
	}

	// 加载今日访问量
	private static void loadToadyNum() {
		// TODO Auto-generated method stub
		DBConnect db = null;
		ResultSet rs = null;
		if (today == null)
			today = getTodayDate();
		String sql = "select vnum from " + TABLE_NAME + " where vdate='"
				+ today + "'";
		try {
			db = new DBConnect(sql);
			rs = db.pst.executeQuery();//执行语句,得到结果集  
			if (rs.next()) {
				today_num = rs.getLong("vnum");
			} else {
				sql = "insert into " + TABLE_NAME + "(vdate,vnum) values('"
						+ today + "',0)";
				db.pst.executeUpdate(sql);
				today_num = 0;
			}
		} catch (Exception e) {
			// TODO: handle exception
			today_num = 0;
			System.out
					.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
		}
	}

	// 加载总访问量
	private static void loadTotalNum() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		DBConnect db = null;
		ResultSet rs = null;
		if (today == null)
			today = getTodayDate();
		String sql = "select vnum from " + TABLE_NAME + " where id=1";
		try {
			db = new DBConnect(sql);
			rs = db.pst.executeQuery();//执行语句,得到结果集  
			if (rs.next()) {
				total_num = rs.getLong("vnum");
			} else {
				total_num = 0;
			}
		} catch (Exception e) {
			// TODO: handle exception
			total_num = 0;
			System.out
					.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
		}
	}

	// 增加总的访问量
	private static int incTotalCounter() {
		int k = 0;
		DBConnect db = null;
		loadNum();
		total_num = total_num + 1;
		String sql = "update " + TABLE_NAME + " set vnum=" + total_num
				+ " where id=1";
		try {
			db = new DBConnect(sql);
			k = db.pst.executeUpdate(sql);
		} catch (Exception e) {
			// TODO: handle exception
			System.out
					.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
		}
		return k;
	}

	// 增加今日的访问量
	public static int incTodayCounter() {
		int k = 0;
		DBConnect db = null;
		String sql = null;
		loadNum();
		today_num += 1;
		sql = "update " + TABLE_NAME + " set vnum=" + today_num
				+ " where vdate='" + today + "'";
		try {
			db = new DBConnect(sql);
			k = db.pst.executeUpdate(sql);
			if (k > 0)
				incTotalCounter();
		} catch (Exception e) {
			// TODO: handle exception
			System.out
					.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
		}
		return k;
	}

	// 获得今天的日期
	private static String getTodayDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	// /获得今日访问量
	public static long getTodayNum() {
		loadNum();
		return today_num;
	}

	// 获得总的访问量
	public static long getTotalNum() {
		loadNum();
		return total_num;
	}
}
最近下载更多
何松林  LV7 2021年10月22日
490127488  LV2 2020年11月12日
8战魂5无双8  LV43 2020年8月3日
Limiun  LV6 2020年3月25日
1352090  LV9 2019年12月10日
夙興夜寐  LV3 2019年11月4日
kong.yee  LV40 2019年8月1日
luyuhan  LV1 2019年7月19日
1459897691  LV7 2019年5月20日
1037352843  LV1 2019年3月7日
最近浏览更多
SJIGNKLI  LV2 2022年12月12日
胖嘟嘟2580  LV3 2022年6月8日
brucega  LV3 2022年1月17日
学习指南  LV5 2022年1月3日
1532593037  LV8 2021年12月20日
hwhrr123  LV3 2021年11月20日
何松林  LV7 2021年10月22日
OUSHUREN 2021年8月3日
暂无贡献等级
hekewen1  LV7 2021年5月17日
zhaojialiang  LV7 2021年5月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友