隨筆 - 59, 文章 - 4, 評論 - 184, 引用 - 7
          數據加載中……

          [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)

          在前面說明過使用Script數據源來獲得web service數據源的做法,在實際操作中,發現雖然有BIRT的幫助文件,但同事對BIRTScript數據源的使用還是不太理解,于是寫出下文以便幫助使用BIRT的高級特性

           

          熟悉了BIRTScript數據源之后,你會感嘆BIRT功能之強大,BIRT團隊承諾在2.0中加入對數據庫連接池的支持,但目前為止,我們還只能通過Script數據源來支持連接池。

           

          為了能夠自定義數據集合以及支持分頁查詢、多表查詢、數據庫連接池或者在DAO中使用Spring+Hibernate或從web Service獲取數據等高級特性,我們需要使用BIRTScript數據源來獲得數據

          下面通過一個示例說明如何使用BIRTScript數據源來通過POJO獲取數據:

           

          注:

          為了使例子不至于因為過于簡單而無法說明情況(如同BIRTTutorial那樣),在這里我使用了一個簡單但完整的DAO層,可直接在項目中使用,同時也為避免過于復雜,本例中沒有使用Spring+HibernateWeb Service獲得數據源,但從POJO中可很簡單的將其改為SH組合或WS

          一、一個簡單的數據庫訪問層

           

          在開始我們浪費些時間來描述一下DAO層的幾個類,以便后面在BIRT中使用它時有所了解。

          首先在Eclipse中建立一個Tomcat項目,然后在src中建立一個com.bat.afp.DAOComm包用來封裝一個非常簡單的DAO類,如下:

          o_1.jpg
          其中
          DBUtil為數據庫連接類(數據庫為Oracle8),使用了DBCP作為數據庫連接池,并使用XML文件(dbconfig.xml)來配置數據庫連接池的信息

           

          DBUtil代碼如下:

            1package com.bat.afp.DAOComm;
            2
            3import java.io.File;
            4import java.net.URL;
            5import java.sql.Connection;
            6import java.sql.DriverManager;
            7import java.sql.SQLException;
            8import org.apache.commons.dbcp.DriverManagerConnectionFactory;
            9import org.apache.commons.dbcp.PoolableConnectionFactory;
           10import org.apache.commons.dbcp.PoolingDriver;
           11import org.apache.commons.pool.impl.GenericObjectPool;
           12import org.apache.log4j.BasicConfigurator;
           13import org.apache.log4j.Logger;
           14import org.dom4j.Document;
           15import org.dom4j.DocumentException;
           16import org.dom4j.Element;
           17import org.dom4j.io.SAXReader;
           18
           19/**
           20 * @author liuyf
           21 */

           22public class DBUtil {
           23
           24    private static final Logger    logger        = Logger.getLogger(DBUtil.class);
           25
           26    private static DBUtil        instance;
           27
           28    private GenericObjectPool    connectionPool;
           29
           30    private static String        dbUrl;
           31
           32    private static String        user;
           33
           34    private static String        password;
           35
           36    private static int            connNumber    = 10;
           37    static
           38    {
           39        BasicConfigurator.configure();
           40        try
           41        {
           42            readConfig();
           43        }

           44        catch (DocumentException e)
           45        {
           46            e.printStackTrace();
           47        }

           48    }

           49
           50    private DBUtil()
           51    {
           52        try
           53        {
           54            initConnectionPool();
           55        }

           56        catch (SQLException e)
           57        {
           58            e.printStackTrace();
           59        }

           60        logger.debug("DBUtil init");
           61    }

           62
           63    /**
           64     * 讀取配置文件
           65     * 
           66     * @throws DocumentException
           67     */

           68    private static void readConfig() throws DocumentException
           69    {
           70        URL url = DBUtil.class.getClassLoader().getResource("dbconfig.xml");
           71        File file = new File(url.getFile());
           72        // File file = new File("dbconfig.xml");
           73        SAXReader reader = new SAXReader();
           74        Document document = reader.read(file);
           75        Element root = document.getRootElement();
           76        Element dbinfo = root.element("dbinfo");
           77        dbUrl = dbinfo.elementText("url");
           78        user = dbinfo.elementText("user");
           79        password = dbinfo.elementText("pwd");
           80        String numStr = dbinfo.elementText("connNumber");
           81        if (numStr != null)
           82            connNumber = Integer.parseInt(numStr);
           83    }

           84
           85    public static DBUtil getInstance()
           86    {
           87        if (instance == null)
           88            return instance = new DBUtil();
           89        else
           90            return instance;
           91    }

           92
           93    /**
           94     * 
           95     */

           96    private void initConnectionPool() throws SQLException
           97    {
           98        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
           99        connectionPool = new GenericObjectPool(null, connNumber);
          100        DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbUrl, user,
          101                password);
          102        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
          103                connectionFactory, connectionPool, nullnullfalsetrue);
          104        PoolingDriver driver = new PoolingDriver();
          105        driver.registerPool("afpdb", connectionPool);
          106    }

          107
          108    public Connection getConnection() throws SQLException
          109    {
          110        return DriverManager.getConnection("jdbc:apache:commons:dbcp:afpdb");
          111    }

          112}

          113

          posted on 2005-09-06 13:26 fisher 閱讀(6938) 評論(7)  編輯  收藏 所屬分類: Eclipse Tech

          評論

          # re:使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          用JDBC方式到是挺簡單的, 用了ScriptDataSet后,
          在從ScriptDataSet的fetch事件里不知道怎么訪問
          主ScriptDataSet的當前DataRow?
          請賜教啊!

          by shinwell
          2005-09-08 23:26 | shinwell

          # re: [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          抱歉,我們的報表沒有用到子報表,所以這部分沒看過
          當初用ScriptDataSet主要是為了支持報表元數據的動態生成
          以及解決大數據量的速度問題

          2005-09-10 21:28 | fisher

          # re: [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          找到需要的資料,留言對作者表示感謝!
          2006-06-27 16:14 | lostdog

          # re: [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          :)
          2006-06-29 20:02 | fisher

          # birt使用JDBC數據源和腳本數據源,哪個效率更高呢?  回復  更多評論   

          birt使用JDBC數據源和腳本數據源,哪個效率更高呢?
          2006-09-22 10:49 | bao-ya

          # re: [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          JDBC數據源每次都要重新連接一次數據庫,而腳本數據源可以自己在數據源內使用數據庫連接池,所以腳本數據源明顯效率高于JDBC數據源
          2006-10-30 22:25 | fisher

          # re: [BIRT]-[Tutorial]-使用ScriptDataSet從POJO中獲得數據(一)  回復  更多評論   

          能不能給一個birt集成spring + struts2 + hibernate 的例子啊!!!
          3Q
          2008-11-02 18:29 | datalong
          主站蜘蛛池模板: 屏东县| 剑阁县| 山西省| 浦江县| 江阴市| 大同市| 涿鹿县| 大埔区| 辽源市| 黑龙江省| 达拉特旗| 天津市| 仁寿县| 宜昌市| 沁源县| 祁阳县| 喀喇| 海阳市| 伽师县| 信阳市| 册亨县| 延寿县| 衡阳市| 定结县| 林芝县| 延长县| 吴桥县| 黑龙江省| 康保县| 西青区| 深泽县| 禄劝| 长海县| 肃宁县| 张家口市| 蛟河市| 元江| 威信县| 武夷山市| 鄂伦春自治旗| 镇雄县|