java定时器

 

package com.erry.util;

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.erry.mag.controller.ClientManager;
import com.erry.mag.controller.OrderManager;
import com.erry.mag.model.ClientLog;

/** *//**
 * 定时器测试
 * @author 
 */
public class Test{
 private TimeTast test;
 public HibernateGenericDAO dao;
    public TimeTast getTest() {
  return test;
 }
 public void setTest(TimeTast test) {
  this.test = test;
 }

 public HibernateGenericDAO getDao() {
  return dao;
 }
 public void setDao(HibernateGenericDAO dao) {
  this.dao = dao;
 }
 public void execute(){
        Timer timer = new Timer();
        TimeTast myTimeTask = new TimeTast();       
        Date date = new Date();
        SimpleDateFormat  formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        String da = formatter1.format(date);
       
        String birth = da+"14:51:00";
     try {
    SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
     //String regtime = formatter.format();
      date =formatter.parse(birth);
    } catch (ParseException e) {
     System.out.print("dateError");
     e.printStackTrace();
    }
  System.out.print("启动的时间"+date);    
       
  long timestamp = 60000;
        /** *//**
         * myTimeTask 指定执行的线程 date 指定第一次日期执行的时间 timestamp 指定每格多久再次执行一次
         */
        timer.schedule(test, date);
    }

    public static void main(String[] args){
        Test t = new Test();
        t.execute();
    }
   
   
}

 

 

package com.erry.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.TimerTask;

import com.erry.mag.model.ClientLog;

public class TimeTast extends TimerTask {
    // 在这里写你要定时执行的方法
 public HibernateGenericDAO dao;
    public void run(){ 
//      Date date1 = new Date();
//         String birth = "11:16:00";
//         date1=   this.changeDate(birth);
//   System.out.print("启动的时间"+date1);    
//   
//    Date date2 = new Date();
//         String birthS ;
//      SimpleDateFormat  formatter2 = new SimpleDateFormat("hh:mm:ss");
//    //String regtime = formatter.format();
//  birthS =formatter2.format(date2);
//   date2=   this.changeDate(birthS);
//   System.out.print("启动的时间"+date2);
//    if(date1.equals(date2)){ 
      String hql ="select distinct name from com.erry.mag.model.ClientIp  ";    //hql查询去除重复的结果集
     
 
//   String hql ="select distinct o.name from com.erry.mag.model.ClientIp o ";
    Collection list= dao.findByOQL(hql);
    ClientLog clientLog = new ClientLog();
    clientLog.setClientDate(new Date());
    clientLog.setLoginNum(new Long(list.size()));
    this.dao.saveOrUpdate(clientLog);
   
//    Date date = new Date();
//    SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd");
// String regtime = formatter.format(date);
//  String dates =formatter.format(date);
//    String hql2 ="from com.erry.mag.model.ClientIp o where to_char(o.loginTime,'yyyy-mm-dd')>=:dates";
    String hql3 =" from com.erry.mag.model.ClientIp  ";
    this.dao.removeAll(hql3);
//   String sql ="select distinct t.name from t_client_ip t";
//   try {
// dao.executeSql(sql);
//} catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
//}
//     Double b = (Double)dao.findOneByOQL(Hql, paramMap);
     
// List list = dao.findListByNativeAPI(sql, new String[]{"t"}, new Class[]{ClientIp.class});

//   ClientIp client = (ClientIp) list.iterator();
//        System.out.print(client.getName());
//    System.out.print(list.size());
      System.out.println(new Date());
//    }
        System.out.println("访问量统计执行完成!");
    }
    public Date changeDate(String ss){
     Date date = new Date();
     try {
    SimpleDateFormat  formatter = new SimpleDateFormat("hh:mm:ss");
     //String regtime = formatter.format();
      date=formatter.parse(ss);
    } catch (ParseException e) {
     System.out.print("dateError");
     e.printStackTrace();
    }
    return date;
    }
 public void setDao(HibernateGenericDAO dao) {
  this.dao = dao;
 }
}


 

你可能感兴趣的:(java,DAO,sql)