Quartz例子

调用代码

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

import com.paic.pafa.app.lwc.core.util.DevLog;

public class Test {

	/**
	 * @param args
	 */
    public static void main(String[] args) { 
        //1、创建JobDetial对象 
        JobDetail jobDetail = new JobDetail(); 
        //设置工作项 
        jobDetail.setJobClass(MyJob.class); 
        jobDetail.setName("MyJob_1"); 
        jobDetail.setGroup("JobGroup_1"); 

        //2、创建Trigger对象 
        SimpleTrigger strigger = new SimpleTrigger(); 
        strigger.setName("Trigger_1"); 
        strigger.setGroup("Trigger_Group_1"); 
        strigger.setStartTime(new Date()); 
        //设置重复停止时间,并销毁该Trigger对象 
        java.util.Calendar c = java.util.Calendar.getInstance(); 
        c.setTimeInMillis(System.currentTimeMillis() + 10000 * 1L); 
        //strigger.setEndTime(c.getTime()); 
        strigger.setFireInstanceId("Trigger_1_id_001"); 
        //设置重复间隔时间 
        strigger.setRepeatInterval(3000 * 10 * 1L); 
        
        //设置重复执行次数 
        strigger.setRepeatCount(strigger.REPEAT_INDEFINITELY); 
        

        //3、创建Scheduler对象,并配置JobDetail和Trigger对象 
        SchedulerFactory sf = new StdSchedulerFactory(); 
        Scheduler scheduler = null; 
        try { 
                scheduler = sf.getScheduler(); 
                scheduler.scheduleJob(jobDetail, strigger); 
                //4、并执行启动、关闭等操作 
                scheduler.start(); 

        } catch (SchedulerException e) { 
                e.printStackTrace(); 
        } 
        
        
//        try { 
//                //关闭调度器 
//                scheduler.shutdown(true); 
//        } catch (SchedulerException e) { 
//                e.printStackTrace(); 
//        } 
}

JOB代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/** 
* quartz定时器测试 
* 
* @author leizhimin 2009-7-23 8:49:01 
*/ 

public class MyJob implements Job {
	public void execute(JobExecutionContext jobExecutionContext)
			throws JobExecutionException {
		System.out.println("begin:" + new Date() );
		
		Connection con = null;// 创建一个数据库连接
	    PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
	    ResultSet result = null;// 创建一个结果集对象
	    int intDML = 0;
		
	    String orgWords = "haha";
	    String newWords = "***";
	    String tmpStr = "";
	    
	    try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url="jdbc:oracle:thin:@xxxxxxxx:1526:xxx"; //orcl为数据库的SID 
			String userName = "xxx";
			String passWord = "xxx";
			
			String sql = "SELECT * FROM  nbu_notice_info i where i.notice_state =?";
			
			con= DriverManager.getConnection(url,userName,passWord);
			System.out.println("connect sucessfully...");
			pre = con.prepareStatement(sql);
			pre.setString(1, "Y");
			result = pre.executeQuery();
			System.out.println("query sucessfully...");
			String content = ""; 
			String primaryKey = "";
			
			
			int index = -1 ;
			while (result.next()) {
				primaryKey = result.getString(1);
				content = result.getString(3);
	            System.out.println("content = " + content );
	            if ((index = content.indexOf(orgWords)) != -1){
	            	System.out.println("存在敏感信息" + orgWords);
	            	tmpStr = content.substring(0,index) 
	            			+ newWords 
	            			+ content.substring(index + orgWords.length(),content.length());
	            	System.out.println("modify sucessfully... \n\ttmpStr = " + tmpStr);
	            	
	            	sql = "UPDATE nbu_notice_info I SET i.notice_content = ? WHERE i.id_notice_sequence= ?";
	            	
	            	pre = con.prepareStatement(sql);
	            	pre.setString(1, tmpStr);
	            	pre.setString(2, primaryKey);
	            	intDML = pre.executeUpdate();
	            	System.out.println("DML执行结果:" + intDML + "行数据已修改!");
	            }   
	        }
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				con.close();
				System.out.println("end:" + new Date() );
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	public static void main(String[] args) { 

	}
}


你可能感兴趣的:(oracle,quartz)