zoukankan      html  css  js  c++  java
  • JAVA TIMER定时器

    备注:类实现ServletContextListener,在web.xml配置,之后服务启动该定时器类自动加载

    package com.leadlt.common.util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;

    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;

    public class TestTimer implements ServletContextListener{
    public static void doCon() throws Exception, Exception, Exception {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    // 获得Oracle驱动的实例
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    // 获得连接对象(提供:地址,用户名,密码)
    con = DriverManager.getConnection(
    "jdbc:oracle:thin:@192.168.1.121:1521:orcl", "parking",
    "parking");

    if (!con.isClosed())
    System.out.println("Successfully connected ");
    else
    System.out.println("failed connected");

    // 建立一个Statement,数据库对象
    st = con.createStatement();
    // 运行SQL查询语句
    //rs = st.executeQuery("select * from DAY_TRAFFIC");
    //rs=st.executeQuery("insert into DAY_TRAFFIC (parkno,time1,time2,ID,types,day_time) VALUES (parkno,0,0,DAY_TRAFFIC_SEQ.nextval,'year',sysdate)");
    rs=st.executeQuery("insert into DAY_TRAFFIC (parkno,ID) VALUES ('0',7)");
    System.out.println(rs);
    // 读取结果集
    while (rs.next()) {
    System.out.println("parkno:" + rs.getString(1));
    /*
    * System.out.println("time1:" + rs.getInt(2));
    * System.out.println("time2:" + rs.getInt(3));
    * System.out.println("time3:" + rs.getInt(4));
    * System.out.println("time4:" + rs.getInt(5));
    * System.out.println("time5:" + rs.getInt(6));
    * System.out.println("time6:" + rs.getInt(7));
    * System.out.println("time7:" + rs.getInt(8));
    * System.out.println("time8:" + rs.getInt(9));
    * System.out.println("time9:" + rs.getInt(10));
    * System.out.println("time10:" + rs.getInt(11));
    * System.out.println("time11:" + rs.getInt(12));
    * System.out.println("time12:" + rs.getInt(13));
    * System.out.println("day_time:" + rs.getDate(26));
    * System.out.println("ID:" + rs.getInt(27));
    * System.out.println("types:" + rs.getString(28));
    */
    }
    // 关闭链接
    con.close();
    // run();
    }

    static int count = 0;
    public static void showTimer() {
    TimerTask task = new TimerTask() {
    @Override
    public void run() {
    ++count;
    System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
    try {
    doCon();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    };

    // 设置执行时间
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);// 每天
    // 定制每天的12:15:15执行,
    calendar.set(year, month, day, 12, 15, 15);
    Date date = calendar.getTime();
    Timer timer = new Timer();
    System.out.println(date);

    int period = 2 * 1000;
    // 每天的date时刻执行task,每隔2秒重复执行
    timer.schedule(task, date, period);
    // 每天的date时刻执行task, 仅执行一次
    // timer.schedule(task, date);
    }

    /*public static void main(String[] args) {
    showTimer();
    }*/

    public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub

    }

    public void contextInitialized(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    showTimer();

    }
    }

  • 相关阅读:
    ES6(四)字符串的扩展
    ES6(三)数组的扩展
    ES6(二) Destructuring-变量的解构赋值
    ES6(一) let and const
    canvas(七) 文字编写
    canvas(五)createPattern
    canvas(四) Gradient- demo
    canvas(三) star- demo
    jQuery之简单动画效果
    web开发小白之路
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/5771760.html
Copyright © 2011-2022 走看看