📄 counter.java~9~
字号:
package dsp.web;/** * <p>Title: DSP实验室</p> * <p>Description: DSP教学网 * 计数器 * </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author 李艳生 * @version 1.0 */import dsp.database.*;import dsp.utils.*;import java.sql.*;public class Counter { String createDate,todayDate,maxDate; int count,todayCount,maxCount; public Counter(String createDate,int count,String todayDate,int todayCount,String maxDate,int maxCount) { this.createDate = createDate; this.count = count; this.todayDate = todayDate; this.todayCount = todayCount; this.maxDate = maxDate; this.maxCount = maxCount; } public int getCount() { return count; } public String getCreateDate() { return createDate; } public int getMaxCount() { return maxCount; } public String getMaxDate() { return maxDate; } public int getTodayCount() { return todayCount; } public String getTodayDate() { return todayDate; } public void setTodayDate(String todayDate) { this.todayDate = todayDate; } public void setTodayCount(int todayCount) { this.todayCount = todayCount; } public void setMaxDate(String maxDate) { this.maxDate = maxDate; } public void setMaxCount(int maxCount) { this.maxCount = maxCount; } public void setCreateDate(String createDate) { this.createDate = createDate; } public void setCount(int count) { this.count = count; } /** 初始化计数器 * */ public static void init() throws ClassNotFoundException,SQLException{ Operation conn = new Operation(); String date = GetDate.getStringDateShort(); String sql = "INSERT INTO web_counter(id, createDate, counter, todayDate, todayCounter, maxDate, maxCounter) "+ "VALUES(1, '"+date+"',0,'"+date+"', 0, '"+date+"',0)"; try{ conn.update(sql); } catch(Exception e){ System.err.print("counter init error:" + e.getMessage()); } finally{ conn.closestmt(); conn.closeconn(); } } /** 计数器加1 * */ public void add() throws ClassNotFoundException, SQLException{ Operation conn = new Operation(); String sql = "SELECT * FROM web_counter WHERE id=1"; ResultSet rs = null; try{ rs = conn.query(sql); if(!rs.next()){ init(); return; } sql = "UPDATE web_counter SET counter = counter + 1, todayCounter = todayCounter+1 WHERE id=1"; conn.update(sql); } catch(Exception e){ System.err.print("counter add error:" + e.getMessage()); } finally{ conn.closestmt(); conn.closeconn(); } } /** 得到计数器 * */ public static Counter getCounter() throws ClassNotFoundException,SQLException{ Operation conn = new Operation(); String sql = "SELECT * FROM web_counter WHERE id=1"; ResultSet rs = null; Counter counter = null; String date = GetDate.getStringDateShort(); try{ rs = conn.query(sql); if(rs.next()){ counter = new Counter(rs.getString("createDate"), rs.getInt("counter"), rs.getString("todayDate"), rs.getInt("todayCounter"), rs.getString("maxDate"), rs.getInt("maxCounter") ); } else{ counter = new Counter(date, 0, date, 0, date, 0); init(); } } catch(Exception e){ System.err.print("counter add error:" + e.getMessage()); } finally{ conn.closestmt(); conn.closeconn(); } return counter; } /**更新计数器 * */ public static void update(String today) throws ClassNotFoundException,SQLException{ Operation conn = new Operation(); String sql = "SELECT * FROM web_counter WHERE id=1"; ResultSet rs = null; try{ rs = conn.query(sql); if(!rs.next()){ init(); return; } sql = "UPDATE web_counter SET maxDate=todayDate, maxCounter=todayCounter+1 where todayCounter>maxCounter AND id=1"; conn.update(sql); sql = "UPDATE web_counter SET todayDate='"+today+"',todayCounter=1 where todayDate <'"+today+"'"; conn.update(sql); } catch(Exception e){ System.err.print("counter add error:" + e.getMessage()); } finally{ conn.closestmt(); conn.closeconn(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -