<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    2008年9月6日

    /**
     * Excel WorkBook對象類
     * @author zhouqian
     *
     */
    public class ExcelWorkBook {
     /** 工作簿 */
     private static HSSFWorkbook workbook = new HSSFWorkbook();
     
     /** 工作表 */
     private HSSFSheet sheet;
     
     /** 表頭數據 */
     private String[] tableHeader;
     
     /**
      * 構造函數
      * @param workbookName 工作表名
      * @param tableHeader 表頭數據
      */
     public ExcelWorkBook(String workbookName, String[] tableHeader) {
      super();
      this.tableHeader = tableHeader;
      sheet = workbook.createSheet(workbookName);
     }
     
     /**
      * 創建表頭
      * @param headerName
      */
     public void createTableHeader(String headerName) {
      HSSFHeader header = sheet.getHeader();
      header.setCenter(headerName);
      HSSFRow headerRow = sheet.createRow(0);
      int cellNumber = tableHeader.length;
      for (int i = 0; i < cellNumber; i++) {
       HSSFCell headerCell = headerRow.createCell((short)i);
       headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
       headerCell.setCellValue(tableHeader[i]);
      }
     }
     
     /**
      * 創建行
      * @param data 要寫入的數據
      * @param rowIndex 第rowIndex行
      */
     public void createTableRow(String[] data, int rowIndex) {
      HSSFRow row = sheet.createRow(rowIndex);
      for (int i = 0; i < data.length; i++) {
       HSSFCell cell = row.createCell((short)i);
       cell.setEncoding(HSSFCell.ENCODING_UTF_16);
       cell.setCellValue(data[i]);
      }
      
     }
     
     /**
      * 創建整個Excel表
      * @param headerName
      */
     public void createExcelSheet(String headerName, String[][] data) {
      this.createTableHeader(headerName);
      
      String[] rowData;
      for (int i = 0; i < data.length; i++) {
       rowData = data[i];
       createTableRow(rowData, i + 1);
      }
      
     }
     
     /**
      * 導出Excel表格
      * @param os
      * @throws IOException
      */
     public void exportExcel(OutputStream os) throws IOException {
      sheet.setGridsPrinted(true);
      HSSFFooter footer = sheet.getFooter();
      footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
      workbook.write(os);
     }

     public HSSFSheet getSheet() {
      return sheet;
     }

     public void setSheet(HSSFSheet sheet) {
      this.sheet = sheet;
     }

     public String[] getTableHeader() {
      return tableHeader;
     }

     public void setTableHeader(String[] tableHeader) {
      this.tableHeader = tableHeader;
     }
    }

    posted @ 2009-03-10 20:27 chou 閱讀(182) | 評論 (0)編輯 收藏
     

    /**
     * 郵件客戶端
     *
     * @author zhouqian
     *
     */
    public class MailClient {
     /** 日志實例 */
     Log logger = LogFactory.getLog(MailClient.class);

     /** 消息對象 */
     private Message message;

     /** 郵件會話 */
     private Session session;

     public MailClient() {
      super();
      this.session = createDefaultSession();
     }

     public MailClient(Session session) {
      super();
      this.session = session;
     }

     public MailClient(Message message) {
      this();
      this.message = message;
     }

     public MailClient(Session session, Message message) {
      super();
      this.session = session;
      this.message = message;
     }

     /**
      * 創建郵件會話
      *
      * @return
      */
     protected Session createDefaultSession() {
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.163.com");
      props.put("mail.smtp.auth", "true");
      Session session = Session.getInstance(props);
      session.setDebug(true);
      return session;
     }

     /**
      * 創建純文本郵件
      *
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      * @return
      * @throws MessagingException
      */
     protected Message createMimeMessage(String subject, String content,
       String recipientTO, String recipientCC, String recipientBCC)
       throws MessagingException {
      Message message = createBlankMessage(recipientTO, recipientCC,
        recipientBCC);

      // 設置郵件標題
      message.setSubject(subject);

      // 設置郵件內容
      message.setText(content);

      // 設置發送時間
      message.setSentDate(new Date(System.currentTimeMillis()));

      // 存儲郵件信息
      message.saveChanges();

      return message;
     }

     /**
      * 創建帶HTML內容的郵件
      *
      * @param subject
      * @param content
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      * @return
      * @throws MessagingException
      */
     protected Message createHTMLMessage(String subject, String content,
       String recipientTO, String recipientCC, String recipientBCC)
       throws MessagingException {
      Message message = createBlankMessage(recipientTO, recipientCC,
        recipientBCC);

      // 設置郵件標題
      message.setSubject(subject);

      // 設置發送時間
      message.setSentDate(new Date(System.currentTimeMillis()));

      // 創建存放郵件內容的BodyPart對象
      BodyPart bp = new MimeBodyPart();
      bp.setContent(content, "text/html;charset=gb2312");

      // 創建一個MimeMultipart來存放BodyPart對象
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(bp);

      message.setContent(mp);
      message.saveChanges();

      return message;
     }

     /**
      * 創建帶附件的郵件
      *
      * @param subject
      * @param content
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      * @return
      * @throws MessagingException
      */
     protected Message createAttachMessage(String subject, String content,
       File attachment, String recipientTO, String recipientCC,
       String recipientBCC) throws MessagingException {
      Message message = createBlankMessage(recipientTO, recipientCC,
        recipientBCC);

      // 設置郵件標題
      message.setSubject(subject);

      // 設置發送時間
      message.setSentDate(new Date(System.currentTimeMillis()));

      // 創建存放郵件內容的BodyPart對象
      BodyPart bp = new MimeBodyPart();
      bp.setContent(content, "text/html;charset=gb2312");

      // 創建一個MimeMultipart來存放BodyPart對象
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(bp);

      // 設置郵件的附件
      bp = new MimeBodyPart();
      FileDataSource fds = new FileDataSource(attachment.getName());
      DataHandler dh = new DataHandler(fds);
      try {
       bp.setFileName(new String(attachment.getName().getBytes("gb2312")));
      } catch (UnsupportedEncodingException e) {
       final String errMess = "Caught exception while encoding file name:"
         + attachment.getName();
       logger.error(errMess);
       throw new MailException(errMess, e);
      }
      bp.setDataHandler(dh);
      mp.addBodyPart(bp);

      message.setContent(mp);
      message.saveChanges();
      return message;
     }

     /**
      * 創建空白郵件
      *
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      * @return
      * @throws MessagingException
      */
     protected Message createBlankMessage(String recipientTO,
       String recipientCC, String recipientBCC) throws MessagingException {
      Message message = new MimeMessage(session);

      // 設置發件人
      InternetAddress from = new InternetAddress("mfktfp2004@163.com");
      message.setFrom(from);

      // 設置收件人
      InternetAddress to = new InternetAddress(recipientTO);
      message.setRecipient(Message.RecipientType.TO, to);
      if (StringUtil.isNotEmpty(recipientCC)) {
       InternetAddress cc = new InternetAddress(recipientCC);
       message.setRecipient(Message.RecipientType.CC, cc);
      }
      if (StringUtil.isNotEmpty(recipientBCC)) {
       InternetAddress bcc = new InternetAddress(recipientBCC);
       message.setRecipient(Message.RecipientType.BCC, bcc);
      }

      return message;
     }

     /**
      * 發送郵件
      *
      * @param message
      * @throws MessagingException
      */
     public void sendEmail(Message message) throws MessagingException {
      // 以smtp方式登陸郵箱
      Transport transport = session.getTransport("smtp");
      transport.connect("smtp.163.com", "mfktfp2004", "19850921"); // SMTP地址,用戶名,密碼

      // 發送郵件
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();
     }

     /**
      * 發送純文本郵件
      *
      * @param subject
      * @param content
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      */
     public void sendMimeEmail(String subject, String content,
       String recipientTO, String recipientCC, String recipientBCC) {
      Message message = null;
      try {
       message = createMimeMessage(subject, content, recipientTO,
         recipientCC, recipientBCC);
       sendEmail(message);
      } catch (MessagingException e) {
       logger.error("Send mime email failure", e);
       throw new MailException("Error sending email, failure", e);
      }

     }

     /**
      * 發送帶HTML內容的郵件
      *
      * @param subject
      * @param content
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      */
     public void sendHTMLEmail(String subject, String content,
       String recipientTO, String recipientCC, String recipientBCC) {
      Message message = null;
      try {
       message = createHTMLMessage(subject, content, recipientTO,
         recipientCC, recipientBCC);
       sendEmail(message);
      } catch (MessagingException e) {
       logger.error("Send html email failure", e);
       throw new MailException("Error sending email, failure", e);
      }
     }

     /**
      * 發送帶附件的郵件
      *
      * @param subject
      * @param content
      * @param recipientTO
      * @param recipientCC
      * @param recipientBCC
      */
     public void sendAttachEmail(String subject, String content,
       File attachment, String recipientTO, String recipientCC,
       String recipientBCC) {
      Message message = null;
      try {
       message = createAttachMessage(subject, content, attachment,
         recipientTO, recipientCC, recipientBCC);
       sendEmail(message);
      } catch (MessagingException e) {
       logger.error("Send html email failure", e);
       throw new MailException("Error sending email, failure", e);
      }
     }

     public Message getMessage() {
      return message;
     }

     public void setMessage(Message message) {
      this.message = message;
     }

     public Session getSession() {
      return session;
     }

     public void setSession(Session session) {
      this.session = session;
     }
     
     public static void main(String[] args) {
      MailClient client = new MailClient();
      client.sendMimeEmail("test", "test", "zhouqian1103@163.com", null, null);
     }
    }


    public class MailException extends RuntimeException {
     private static final long serialVersionUID = 1L;
     
     /** Throwable實例 */
     protected Throwable throwable;
     
     public MailException() {
      super();
     }
     
     public MailException(String message) {
      super(message);
     }
     
     public MailException(Throwable cause) {
      this.throwable = cause;
     }
     
     public MailException(String message, Throwable cause) {
      super(message);
      this.throwable = cause;
     }
     
     public void printStackTrace(PrintStream ps) {
      super.printStackTrace(ps);
      if (throwable != null) {
       ps.println("with nested Exception:" + throwable);
       throwable.printStackTrace(ps);
      }
     }
     
     public void printStackTrace(PrintWriter pw) {
      super.printStackTrace(pw);
      if (throwable != null) {
       pw.println("with nested Exception:" + throwable);
       throwable.printStackTrace(pw);
      }
     }
     
     public String toString() {
      if (throwable == null) {
       return super.toString();
      } else {
       return super.toString() + "with nested exception:" + throwable;
      }
     }

     public Throwable getThrowable() {
      return throwable;
     }
    }


    posted @ 2009-03-10 20:26 chou 閱讀(259) | 評論 (0)編輯 收藏
     
    在測試Hibernate的一對多雙向關聯映射時,碰到很有趣的問題,跟inverse屬性直接相關。

    1、People.hbm.xml

    <hibernate-mapping default-lazy="false"> 
    <class name="com.persistent.People" table="people"> 
    <id name="id" column="peopleId" unsaved-value="0"> 
    <generator class="increment"> 
    </generator> 
    </id> 
    <property name="name" column="name"></property> 
    <set name="addresses" cascade="save-update">
    <key column="peopleId" not-null="true" />
    <one-to-many class="com.persistent.Address"/>
    </set> 
    </class>
    </hibernate-mapping>

    2、Address.hbm.xml

    <hibernate-mapping>
    <class name="com.persistent.Address" table="address"> 
    <id name="id" column="addressId" unsaved-value="0">
    <generator class="increment">
    </generator>
    </id> 
    <many-to-one name="people" column="peopleId" insert="false" update="false"></many-to-one> 
    <property name="addressName" column="addressName"></property> 
    <property name="codeNumber" column="codeNumber"></property> 
    </class> 
    </hibernate-mapping>
    3、People.java和Address.java

    public class People ...{ 
    private long id; 
    private String name; 
    private Set addresses = new HashSet(); 
    ...
    }

    public class Address ...{ 
    private long id; 
    private People people; 
    private String addressName; 
    private String codeNumber; 
    ...
    } 

    4、數據庫結構

    people表:{peopleId,name}

    address表:{addressId,peopleId,addressName,codeNumber}

    5、測試代碼

    People people = new People(); 
    people.setName("linda"); 
    Address address = new Address(); 
    address.setAddressName("yunnan"); 
    address.setCodeNumber("564123"); 
    address.setPeople(people); 
    people.getAddresses().add(address); 
    Session session = HibernateSessionFactory.getSession(); 
    session.beginTransaction(); 
    session.save(people); 
    session.getTransaction().commit(); 

    6、運行結果

      上面測試代碼運行起來正確:

    Hibernate: select max(peopleId) from people
    Hibernate: select max(addressId) from address
    Hibernate: insert into people (name, peopleId) values (?, ?)
    Hibernate: insert into address (addressName, codeNumber, peopleId, addressId) values (?, ?, ?, ?)
    Hibernate: update address set peopleId=? where addressId=?

      如果將People.hbm.xml映射改寫一下:

    <set name="addresses" cascade="save-update" inverse="true">
    <key column="peopleId" not-null="true" />
    <one-to-many class="com.persistent.Address"/>
    </set>

      不同之處在于添加了inverse="true",結果:

    Hibernate: select max(peopleId) from people
    Hibernate: select max(addressId) from address
    Hibernate: insert into people (name, peopleId) values (?, ?)
    Hibernate: insert into address (addressName, codeNumber, addressId) values (?, ?, ?)

      可以看到,peopleId并沒有寫入到關聯的address當中,數據庫address表中相應記錄的peopleId字段為空。

    7、分析

      在Hibernate中,術語inverse是反轉的意思,在關聯關系中,inverse="false"為主控方,由主控方負責維護對象的關聯關系。所以上面的映射文件改動之后,address為主控方,people為被控方,但是測試代碼只進行了一個保存操作session.save(people),這是針對people的,因此無法正確級聯保存address。而原來的映射文件中(雖然沒有明確指明,Hibernate默認inverse="false"),people為主控方,因此保存people時它會保證關聯的address的正確保存。

      也就是說,Hibernate僅僅按照主控方對象的狀態的變化來同步更新數據庫。按照原來的映射文件,people.getAddresses().add(address),即主控方對象的狀態發生了改變,因此數據庫會跟著對象狀態的變化來同步更新數據庫;而address.setPeople(people),即被控方對象的狀態發生了改變,它是不能觸發對象和數據庫的同步更新的。
    posted @ 2008-09-06 14:41 chou 閱讀(160) | 評論 (0)編輯 收藏
     
    主站蜘蛛池模板: 亚洲日本人成中文字幕| 亚洲毛片免费观看| 特黄aa级毛片免费视频播放| 毛片A级毛片免费播放| 亚洲国产美女福利直播秀一区二区| 免费一区二区无码东京热| 亚洲三区在线观看无套内射| 人人爽人人爽人人片av免费| 久久久久国产成人精品亚洲午夜| 免费人人潮人人爽一区二区| 2048亚洲精品国产| 中文字幕免费人成乱码中国| 亚洲AV无码成人精品区蜜桃| 久久久久免费看成人影片| 亚洲日本香蕉视频| 在线观看成人免费视频| 蜜芽亚洲av无码一区二区三区| 免费中文字幕一级毛片| 久久国产精品免费一区| 亚洲视频在线观看一区| 国产福利在线免费| 边摸边脱吃奶边高潮视频免费| 国产亚洲精品AA片在线观看不加载 | av成人免费电影| 亚洲AV无码乱码国产麻豆穿越| 国产精品免费精品自在线观看| 国产成人精品日本亚洲网址| 免费人成视频x8x8入口| 久久青草91免费观看| 亚洲无吗在线视频| 亚洲片一区二区三区| 国产精品免费看久久久| 亚洲乱码一区二区三区国产精品| 亚洲Av无码国产情品久久| 日韩电影免费在线观看中文字幕| 亚洲无mate20pro麻豆| 久久久久亚洲av成人无码电影| 8x8×在线永久免费视频| 国产精品亚洲二区在线| 亚洲福利视频一区| 国产一区在线观看免费|