您的位置:首页 > 数据库

Quartz作业调度定时完成数据库操作

2015-07-21 09:45 281 查看
public class QuartzTest {

    public static void main(String args[]) throws SchedulerException, ParseException {

        JobDetail jobDetail= JobBuilder.newJob(TestJob.class)

                .withIdentity("testJob_1","group_1")

                .build();

 

        Trigger trigger= TriggerBuilder

                .newTrigger()

                .withIdentity("trigger_1","group_1")

                .startNow()

                .withSchedule(SimpleScheduleBuilder.simpleSchedule()

                        .withIntervalInHours(24) //时间间隔24小时

                        .withRepeatCount(3)        //重复次数(将执行4次)

                        )

                .build();

        SchedulerFactory sf = new StdSchedulerFactory();

        Scheduler sched = sf.getScheduler();

 

        sched.scheduleJob(jobDetail,trigger);

        sched.start();

    }
}

TestJob.java

public class TestJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("-------Hello World! ------" + new Date());

        //do more...

        Connection connection = null;

        PreparedStatement ps=null;

        ResultSet rs = null;

        try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/emsprodb", "postgres",
"123");
ps = connection.prepareStatement("select * from worktastcreate()");
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}

    }

postgresql的worktastcreate()语句:

CREATE OR REPLACE FUNCTION worktastcreate()

  RETURNS integer AS

$BODY$

     begin

          INSERT INTO work_task(

            code, name_en, name_cn, create_by, update_by, create_datetime, 

            update_datetime, version, spec_code, incharge, assign_by, priority, 

            factory_code, location_code) values('ad', 'cn', 'en','rao','rao', now(), 

            now(), 1, 'xx', 'xx', 'xx', 1, 

            'xx', 'xx');

            return 0;

     end;

$BODY$

  LANGUAGE plpgsql VOLATILE

  COST 100;

ALTER FUNCTION worktastcreate()

  OWNER TO postgres;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  quartz