Java軟件報(bào)表軟件技術(shù)博客

          java報(bào)表軟件技術(shù)匯總 java報(bào)表軟件制作 報(bào)表軟件新聞
          posts - 355, comments - 100, trackbacks - 0, articles - 3
             :: 首頁(yè) :: 新隨筆 ::  :: 聚合  :: 管理

          我們前面講了幾個(gè)數(shù)據(jù)源,今天我們來講一下EJB數(shù)據(jù)源,這篇講完我們數(shù)據(jù)源這部分就講完了。數(shù)據(jù)連接不需要直接訪問數(shù)據(jù)庫(kù),而是使用EJB做為數(shù)據(jù)源。FR通過定義程序數(shù)據(jù)集使用EJB的相關(guān)類獲取到EJB數(shù)據(jù)源,然后轉(zhuǎn)為我們里面的二維表作為報(bào)表數(shù)據(jù)源使用,進(jìn)行展示。例如如下獲取到ejb數(shù)據(jù)所做的模板。

          一、 實(shí)現(xiàn)原理

          FineReport報(bào)表的數(shù)據(jù)來源可以是任何類型的數(shù)據(jù),因?yàn)镕ineReport是通過AbstractTableData抽象類來讀取數(shù)據(jù)源的,因此用戶只要實(shí)現(xiàn)了AbstractTableData抽象類,也就可以用自定義類型的程序數(shù)據(jù)集,F(xiàn)ineReport報(bào)表引擎就能夠讀取定義的數(shù)據(jù)源作為報(bào)表數(shù)據(jù)源使用。AbstractTableData抽象類主要有5個(gè)方法,具體使用可參考報(bào)表引擎API開發(fā)入門—簡(jiǎn)單程序數(shù)據(jù)集。EJB程序數(shù)據(jù)源準(zhǔn)備數(shù)據(jù)使用方法init(),獲取到ejb的javaBean,從而保存數(shù)據(jù)到ArrayList中。

          二、 實(shí)現(xiàn)步驟

          2.1 定義程序數(shù)據(jù)源
          定義DataModelDemo這個(gè)類代碼如下:

          package com.fr.data;
          import javax.naming.*;
          import javax.ejb.*;
          import java.rmi.*;
          import java.util.*;
          import com.fr.data.AbstractTableData;
          import examples.ejb.ejb20.basic.beanManaged.*;
          public class DataModelDemo extends AbstractTableData {
              
          private String[] columnNames;
              
          private ArrayList valueList = null;
              
          public DataModelDemo() {
                  String[] columnNames 
          = { "Name""Score" };
                  
          this.columnNames = columnNames;
              }
              
          // 實(shí)現(xiàn)其他四個(gè)方法
              public int getColumnCount() {
                  
          return columnNames.length;
              }
              
          public String getColumnName(int columnIndex) {
                  
          return columnNames[columnIndex];
              }
              
          public int getRowCount() {
                  init();
                  
          return valueList.size();
              }
              
          public Object getValueAt(int rowIndex, int columnIndex) {
                  init();
                  
          return ((Object[]) valueList.get(rowIndex))[columnIndex];
              }
              
          // 準(zhǔn)備數(shù)據(jù)
              public void init() {
                  
          // 確保只被執(zhí)行一次
                  if (valueList != null) {
                      
          return;
                  }
                  
          // 保存得到的結(jié)果集
                  valueList = new ArrayList();
                  Context ctx 
          = null;
                  Account ac 
          = null;
                  AccountHome home 
          = null;
                  
          try {
                      
          // Contact the AccountBean container (the "AccountHome") through
                      
          // JNDI.
                      ctx = new InitialContext();
                      home 
          = (AccountHome) ctx
                              .lookup(
          "java:/comp/env/BeanManagedAccountEJB");
                      
          double balanceGreaterThan = 100;
                      Collection col 
          = home.findBigAccounts(balanceGreaterThan);
                      
          if (col != null) {
                          
          // 用對(duì)象保存數(shù)據(jù)
                          Object[] objArray = null;
                          Iterator iter 
          = col.iterator();
                          
          while (iter.hasNext()) {
                              Account bigAccount 
          = (Account) iter.next();
                              objArray 
          = new Object[2];
                              objArray[
          0= bigAccount.getPrimaryKey();
                              objArray[
          1= new Double(bigAccount.balance());
                              
          // 在valueList中加入這一行數(shù)據(jù)
                              valueList.add(objArray);
                          }
                      }
                  } 
          catch (Exception ex) {
                      ex.printStackTrace();
                  }
              }
          }

          注:使用之前需要先導(dǎo)入ejb的jar包
          2.2 編譯程序數(shù)據(jù)源
          將編譯后的DataModelDemo.class放到項(xiàng)目的WEB-INF下面的classes目錄下,因?yàn)镈ataModelDemo.java屬于包c(diǎn)om.fr.data,所以DataModelDemo.class需要放到classes\com\fr\data目錄下。
          2.3 配置程序數(shù)據(jù)源
          新建報(bào)表,在報(bào)表數(shù)據(jù)集中新建程序數(shù)據(jù)源,選擇我們定義好的程序數(shù)據(jù)集,如下圖,名字可以自定義,如Pro。

          2.4 使用程序數(shù)據(jù)源
          配置好程序數(shù)據(jù)源后便可以使用定義的Pro程序數(shù)據(jù)集了,與其他類型的數(shù)據(jù)集使用方法是相同的,可以通過拖拽方法實(shí)現(xiàn)單元格數(shù)據(jù)列綁定。如下圖

          保存模板到ejb項(xiàng)目環(huán)境下,啟動(dòng)Examples Server服務(wù)器,預(yù)覽模板就可以成功訪問到模板了!




          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 鄂托克旗| 定南县| 巴林左旗| 庆元县| 突泉县| 甘德县| 密山市| 金阳县| 浪卡子县| 金山区| 公主岭市| 稻城县| 腾冲县| 潼南县| 巧家县| 汉源县| 昌都县| 从江县| 惠水县| 镇康县| 万盛区| 长沙县| 通州市| 江华| 庆阳市| 民县| 宁蒗| 白朗县| 濉溪县| 岳普湖县| 九江市| 洛浦县| 松阳县| 天等县| 万全县| 吴川市| 大邑县| 长宁县| 绥阳县| 陕西省| 宁武县|