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");
}
}
}