SpringMVC定时器配置

本文介绍了一个使用Spring框架实现的定时任务案例,该任务定期检查MySQL数据库中的合同状态,并更新那些已经过期的记录。文章展示了如何配置Spring定时任务及具体的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation=http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd

2.配置文件applicationContext.xml中添加<task:annotation-driven />

3.

@Component
public class RegularlyUpdateContractStateController{
	
	@Scheduled(cron="0 5 0 * * ?")
	public void updateAllRsrContractInfoState(){
		System.out.println("--------------------------------------------------------执行定时Start");
		
		//声明Connection对象
        Connection con;
        //驱动程序名
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://192.168.1.128:3306/qnmb";
        //MySQL配置时的用户名
        String user = "root";
        //MySQL配置时的密码
        String password = "";
        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法,连接MySQL数据库!!
            con = DriverManager.getConnection(url,user,password);
            if(!con.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            //2.创建statement类对象,用来执行SQL语句!!
            Statement statement = con.createStatement();
            //要执行的SQL语句
            String sql = "select * from rsr_contract_info where sign = 1 and (state=1 or state=2) AND DATE_FORMAT(validity_time,'%Y-%m-%d') <= DATE_FORMAT(NOW(),'%Y-%m-%d');";
            //3.ResultSet类,用来存放获取的结果集!!
            ResultSet rs = statement.executeQuery(sql);
             
            String name = null;
            while(rs.next()){
                //获取stuname这列数据
            	//获取ID 修改 state为已失效Constants.CONTRACT_SIGN_STATE_YSX,rejected为 签署超时
            	Statement renshibaost2 = con.createStatement();
				String sql2 = "update rsr_contract_info set state = "+Constants.CONTRACT_SIGN_STATE_YSX+" ,rejected = '签署超时' where id = '"+rs.getString("id")+"'";
				int result = renshibaost2.executeUpdate(sql2);//查询之后返回结果集
				if(result>0){
	                System.out.println("修改已失效数据操作成功");
	            }else{
	                System.out.println("修改已失效数据操作失败");
	            }
            }
            rs.close();
            con.close();
        } catch(ClassNotFoundException e) {   
            //数据库驱动类异常处理
            e.printStackTrace();   
            } catch(SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();  
            }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
        	System.out.println("--------------------------------------------------------执行定时End");
        }
		
	}

	
}

 

转载于:https://my.oschina.net/miwang/blog/1586541

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值