GwylTask.java
/////////////////////////////////////////////
/*****此程序主要用於每日料件單損耗從TITOP上面導過來之後,自動根據
系統中的出貨計劃計算每日關務用量並插入到系統中******/
package listeners;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import com.logistic.ConnectDBPool.*;
import java.sql.*;
public class GwylTask extends TimerTask{


/*********定義一變量規定:此任務在每天凌晨4點執行************/
private static final int scheduleHour=4;
private ResultSet rs=null;//define a result object
/***定義一個標記判斷此任務是否正在執行,如果正在執行的話,則忽略傳遞
過來的執行任務,此判斷目的是防止上次任務還沒有被執行完,而這次任務又
要被執行,但一般一個任務不會24小時,另外注意:此處定義的是靜態變量**/
private static boolean isRunning =false;
private ServletContext context = null;
public GwylTask(ServletContext context){
this.context = context;
}
public void run(){
Calendar cal = Calendar.getInstance();//生成一個日歷對象 
if(!isRunning){//如果上次任務已執行完畢
if(scheduleHour==cal.get(Calendar.HOUR_OF_DAY)) { 
isRunning = true; //設置執行標記為true
context.log("start excuting gwyl tasking!!");//往日志文件中寫入任務開始執行信息
this.importGwylDate();//導入關務用量數據
isRunning = false;//設置執行標記為false
context.log("the special gwyl task end!!");//往日志文件中寫入任務執行完畢信息 

}else{ 
context.log("last gwyl task has not been executed !!");//往日志文件中寫入上次任務還沒執行結束信息
}
}//end function run() 

/*******************************************************************/
/*此函數作用:根據料件單損耗和出貨計劃數據計算每日關務用量並重新導入數據庫中***/
/*******************************************************************/
public void importGwylDate(){
SqlBeanPool db=new SqlBeanPool();//define a connection pool object
try{
db.createConnection();//connect oracle database
db.conn.setAutoCommit(false);//設置自動提交為false
/******************利用簡單事務處理更新數據庫******************/
try{
    /*刪除本地數據庫上關務用量的所有紀錄,此處注意
    不建議使用delete,因為delete不能釋放表空間****/
db.stmt.executeUpdate("truncate table gwyl");
/******根據系統中的出貨計劃和料件單損耗
基本信息自動計算每日的關務用量***/
db.stmt.executeUpdate("insert into gwyl(dzzc_id,gwdm_id,cpba_id,cplh_id,"+
" version_id,ljba_id,gwyl,ch_date,modify_date) "+
" select t1.dzzc_id,t1.gwdm_id,t1.cpba_id,t1.cplh_id,"+
" t1.version_id,t2.ljba_id,t1.ch_num*t2.dxh,t1.ch_date,sysdate"+
" from chjh t1,ljdxh t2 where  t1.dzzc_id=t2.dzzc_id "+
" and t1.cpba_id=t2.cpba_id and t1.version_id=t2.version_id");
db.conn.commit();//提交事務
}catch(Exception e){
context.log("commit failed,the cause :"+e.toString());
try{
db.conn.rollback();
db.freeConnection();
}catch(Exception ex){
context.log("rollback error:"+ex.toString());
}
}
/***** Free connection and other system resource ******/
}catch(Exception e){
context.log("Unexpected error:"+e.toString());
}finally{
try{if(rs!=null){rs.close();rs=null;}}catch(Exception e){}
db.freeConnection();
}//end finally

}//end function importGwkcDate()
  }////end class 

解决方案 »

  1.   

    <%@ page import="javax.mail.*,javax.mail.internet.*,javax.activation.*,java.util.*,java.sql.*"%>
    <%@page contentType="text/html;charset=big5"%>
    <%
    try{
    String smtpHost="10.160.11.18";//smtp主机
    String from ="CP-SCM2B";//發件人
    String[] to={"CP-SCM2B/CPBG/FOXCONN","CP-SCM4B/CPBG/FOXCONN"};//收件人
    String cc="CP-LOG1B/CPBG/FOXCONN";//抄送人
    String bcc ="";//暗送人
    String subject = "呈:2004年机電外包工程費用分析報表.From:運籌物流訊流開發組";//主題
    String body="<font size=3>呈:各主管<P>&nbsp;&nbsp;請點擊右方查看:"+
    "<a href='http://10.160.8.68:8080/jidian/tjbb/bjfx.jsp'>"+
    ""+currentYear+"年度CPBG機電工程費用分析報表</a><br></font><p>"+
    "<font size=2>如果您有什么問題或疑問請tel:78354与訊流開發組人員聯繫!<br><p><p><p><p>"+
    "Best regards<br>**************************</font>";//內容String MailType="text/html";//定義郵件類型為html格式的變量
    MimeMultipart mmp=new MimeMultipart();//to create a mimemultipart 
    MimeBodyPart mbp=new MimeBodyPart();
    Properties props = new Properties();//獲得系統屬性
    Session sendMailSession=Session.getInstance(props, null);//獲取郵件會話對象
    props.put("mail.smtp.host",smtpHost);//設置smtp host
    MimeMessage mMsg = new MimeMessage(sendMailSession);//創建MIME郵件對象
    mMsg.setFrom(new InternetAddress(from));//設置發信人
    InternetAddress[] address=new InternetAddress[to.length];//定義收件人列表數組
    for(int i=0;i<to.length;i++)
      address[i]=new InternetAddress(to[i]);
    mMsg.setRecipients(Message.RecipientType.TO,address);//設置收信人
    mMsg.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));//設置副本抄送人
    mMsg.setSubject(subject,"big5");//設置主題設置主題並進行big5編碼
    mMsg.setSentDate(new java.util.Date());//設置發送日期
    mbp.setContent(body,MailType+";charset=big5");//設置發送內容並進行big5編碼
    mmp.addBodyPart(mbp);
    mMsg.setContent(mmp);
    Transport transport = sendMailSession.getTransport("smtp");
    transport.send(mMsg);
    out.print("發送成功!");
    }catch(Exception e){out.print("Unexpected Error:"+e.toString()+"<br>");}%>
      

  2.   

    这个帖子看得我莫名其妙,一点头绪都没有
    如果楼主是想说明怎么在b/s系统中使用定时程序
    那也应该像这篇文章一样做点说明啊:http://www.kupage.com/wpm/12/20040302/1701290000027kkaxl1y.htm
    《如何在Web工程中实现任务计划调度》
      

  3.   


    package com.logistic.ConnectDBPool;import java.util.*;
    import java.text.*;
    import java.util.Hashtable;
    import java.sql.Timestamp;public class GeneralMethod{
     //除去字符串數據后的空格,
      public  String trim(String str){
       if(str==null){
         return "";
       }else{
         return str.trim();
       }
      }
      public String trim(int str){
       if(str==0){
       return "";
       }else{
           return str+"";
       }
      }
      public String trim(float str){
       if(str==0){
       return "";
       }else{
           return str+"";
       }
      }
      public String trim(double str){
       if(str==0){
       return "";
       }else{
           return str+"";
       }
      }
      public  String trim(Timestamp str){
       if(str==null){
         return "";
       }else{
         return str.toString();
       }
      }  //將日期型數據轉化為格式化字符串 
      public static String dateToStr(Date date){
       SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
       String dateStr=formatter.format(date).toString();
       return dateStr;   
      }
      public static String dateToStr(Date date,String fmt){
       SimpleDateFormat formatter=new SimpleDateFormat(fmt);
       String dateStr=formatter.format(date).toString();
       return dateStr;   
      }  //處理頁面中字符傳化的問題
       public String getStr(String str){
        if(str==null){
        return "";
        }
        try{
          String temp_p=str.trim();
          String temp=new String(temp_p.getBytes("iso-8859-1"),"big5") ;
          return temp;  
        }
        catch(Exception e){
        }
       return null;
      }
      
      
      //格式化數據,參數num為將小數點后的小數位數,如沒有則以"0"填滿
       public String format(double db1,int num){
       
         String formatStr="#,##0.";
         int deciLength;
         for(int i=num;i>0;i--){
            formatStr=formatStr+"0";
         }
         NumberFormat myForm=new DecimalFormat(formatStr);
         String numStr=myForm.format(db1).toString();  
         return numStr;   
          
       }
       public String format(float flt1,int num){
       
         String formatStr="#,##0.";
         int deciLength;
         for(int i=num;i>0;i--){
            formatStr=formatStr+"0";
         }
         NumberFormat myForm=new DecimalFormat(formatStr);
         String numStr=myForm.format(flt1).toString();  
         return numStr;   
          
       }
       
       //格式化數據,整型
       public String format(int db1){
       
         String formatStr="#,###";
         int deciLength;
         NumberFormat myForm=NumberFormat.getInstance();
         String numStr=myForm.format(db1).toString();    
         return numStr;    
       }
       
       //返回指定小數位數的數據值
       public double round(double db1,int num){
         String formatStr="##0.";
         int deciLength;
         for(int i=num;i>0;i--){
            formatStr=formatStr+"#";
         }
         NumberFormat myForm=new DecimalFormat(formatStr);
         String numStr=myForm.format(db1).toString();
         double db2=Double.parseDouble(numStr);
         return db2;
       } 
       public float round(float flt1,int num){
         String formatStr="##0.";
         int deciLength;
         for(int i=num;i>0;i--){
            formatStr=formatStr+"#";
         }
         NumberFormat myForm=new DecimalFormat(formatStr);
         String numStr=myForm.format(flt1).toString();
         float db2=Float.parseFloat(numStr);
         return db2;
       } 
      /* 
      public static void main(String[] args){
         GeneralMethod gm=new GeneralMethod();
         float bb=0.23456789f;
         System.out.println(gm.round(bb,3));
      }
      */
    }