分三步
一、定时执行的任务类Daemon
二、扩展HttpServlet的servlet
三、配置web.xml文件
细节如下:
一、定时执行的任务类Daemon
package com.resoft.recis.common.timer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
/**
* Function:
* User : zxs_9999
* Date :
*/
public class Daemon {
private static final Timer timer = new Timer();
// private static String sDBDriver = "com.ibm.db2.jcc.DB2Driver";
// private static String url="jdbc:db2://127.0.0.1:50000/test";
// private static String user="db2";
// private static String password="db2";
private static String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test";
private static String user="sql";
private static String password="sql";
private static Connection conn = null;
private static ResultSet rs = null;
static{
try {
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.out.println("in Jdbc() , ClassNotFoundException");
e.printStackTrace();
}catch(Exception e){
System.out.println("in Jdbc() , after ClassNotFoundException's Exception");
e.printStackTrace();
}
}
public static void start(){
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("run hour: "+hour);
//20点以后再执行
if(hour>=20 && hour<=22){
ResultSet RS = executeQuery("SELECT * FROM OrgMng");
Date workdate=null;
try {
rs.next();
workdate=rs.getDate("workdate");
RS.close();
executeUpdate("update OrgMng set workdate ='"+changeDate(workdate)+"'");
} catch (Exception e) {
// TODO: handle exception
System.out.println("in start() , Exception");
e.printStackTrace();
}
}
}
}
,0,1000*60*60*2);//两个小时1000*60*60*2 自动执行一次
System.out.println("timer 自动更新日期程序启动...");
}
public static ResultSet executeQuery(String sql) {
rs = null;
try {
conn= DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.out.println("in executeQuery , SQLException");
ex.printStackTrace();
}catch(Exception e){
System.out.println("in executeQuery , after SQLException's Exception");
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(String sql) {
try {
//conn = DriverManager.getConnection(url);
conn= DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(SQLException ex) {
System.out.println("in executeUpdate , SQLException");
ex.printStackTrace();
}catch(Exception e){
System.out.println("in executeUpdate , after SQLException's Exception");
e.printStackTrace();
}
}
public static String changeDate(Date workdate){
String temp=null;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
calendar.setTime(workdate);
calendar.add(Calendar.DATE, 1);
temp=sdf.format(calendar.getTime());
System.out.println("workdate is "+temp);
}catch(Exception e){
System.out.println("in changeDate ,Exception");
e.printStackTrace();
}
return temp;
}
public static void main(String[] args) throws ServletException {
Daemon.start();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
/**
* Function:
* User : zxs_9999
* Date :
*/
public class Daemon {
private static final Timer timer = new Timer();
// private static String sDBDriver = "com.ibm.db2.jcc.DB2Driver";
// private static String url="jdbc:db2://127.0.0.1:50000/test";
// private static String user="db2";
// private static String password="db2";
private static String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test";
private static String user="sql";
private static String password="sql";
private static Connection conn = null;
private static ResultSet rs = null;
static{
try {
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.out.println("in Jdbc() , ClassNotFoundException");
e.printStackTrace();
}catch(Exception e){
System.out.println("in Jdbc() , after ClassNotFoundException's Exception");
e.printStackTrace();
}
}
public static void start(){
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("run hour: "+hour);
//20点以后再执行
if(hour>=20 && hour<=22){
ResultSet RS = executeQuery("SELECT * FROM OrgMng");
Date workdate=null;
try {
rs.next();
workdate=rs.getDate("workdate");
RS.close();
executeUpdate("update OrgMng set workdate ='"+changeDate(workdate)+"'");
} catch (Exception e) {
// TODO: handle exception
System.out.println("in start() , Exception");
e.printStackTrace();
}
}
}
}
,0,1000*60*60*2);//两个小时1000*60*60*2 自动执行一次
System.out.println("timer 自动更新日期程序启动...");
}
public static ResultSet executeQuery(String sql) {
rs = null;
try {
conn= DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException ex) {
System.out.println("in executeQuery , SQLException");
ex.printStackTrace();
}catch(Exception e){
System.out.println("in executeQuery , after SQLException's Exception");
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(String sql) {
try {
//conn = DriverManager.getConnection(url);
conn= DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(SQLException ex) {
System.out.println("in executeUpdate , SQLException");
ex.printStackTrace();
}catch(Exception e){
System.out.println("in executeUpdate , after SQLException's Exception");
e.printStackTrace();
}
}
public static String changeDate(Date workdate){
String temp=null;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
calendar.setTime(workdate);
calendar.add(Calendar.DATE, 1);
temp=sdf.format(calendar.getTime());
System.out.println("workdate is "+temp);
}catch(Exception e){
System.out.println("in changeDate ,Exception");
e.printStackTrace();
}
return temp;
}
public static void main(String[] args) throws ServletException {
Daemon.start();
}
}
二、扩展HttpServlet的servlet
package com.resoft.recis.common.timer;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
/**
* User: zxs_9999
* Date:
* Time:
*/
public class HttpImportDaemon extends HttpServlet{
public void init() throws ServletException {
Daemon.start();
}
}
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
/**
* User: zxs_9999
* Date:
* Time:
*/
public class HttpImportDaemon extends HttpServlet{
public void init() throws ServletException {
Daemon.start();
}
}
三、配置web.xml文件
<servlet>
<servlet-name>workdate</servlet-name>
<display-name>chage workdate</display-name>
<servlet-class>
com.resoft.recis.common.timer.HttpImportDaemon
</servlet-class>
<load-on-startup />
</servlet>
<servlet-mapping>
<servlet-name>workdate</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-name>workdate</servlet-name>
<display-name>chage workdate</display-name>
<servlet-class>
com.resoft.recis.common.timer.HttpImportDaemon
</servlet-class>
<load-on-startup />
</servlet>
<servlet-mapping>
<servlet-name>workdate</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>