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

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

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

           

          熟悉了BIRTScript數(shù)據(jù)源之后,你會(huì)感嘆BIRT功能之強(qiáng)大,BIRT團(tuán)隊(duì)承諾在2.0中加入對數(shù)據(jù)庫連接池的支持,但目前為止,我們還只能通過Script數(shù)據(jù)源來支持連接池。

           

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

          下面通過一個(gè)示例說明如何使用BIRTScript數(shù)據(jù)源來通過POJO獲取數(shù)據(jù):

           

          注:

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

          一、一個(gè)簡單的數(shù)據(jù)庫訪問層

           

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

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

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

           

          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 閱讀(6939) 評論(7)  編輯  收藏 所屬分類: Eclipse Tech

          評論

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

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

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

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

          抱歉,我們的報(bào)表沒有用到子報(bào)表,所以這部分沒看過
          當(dāng)初用ScriptDataSet主要是為了支持報(bào)表元數(shù)據(jù)的動(dòng)態(tài)生成
          以及解決大數(shù)據(jù)量的速度問題

          2005-09-10 21:28 | fisher

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

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

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

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

          # birt使用JDBC數(shù)據(jù)源和腳本數(shù)據(jù)源,哪個(gè)效率更高呢?  回復(fù)  更多評論   

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

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

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

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

          能不能給一個(gè)birt集成spring + struts2 + hibernate 的例子啊!!!
          3Q
          2008-11-02 18:29 | datalong
          主站蜘蛛池模板: 桃江县| 清新县| 阿克苏市| 共和县| 宁陵县| 新兴县| 西乡县| 霸州市| 马关县| 绿春县| 扎兰屯市| 方山县| 五大连池市| 搜索| 阜城县| 灵台县| 获嘉县| 凤城市| 深圳市| 兰坪| 营口市| 荔浦县| 合作市| 扎赉特旗| 遵义市| 新晃| 外汇| 南召县| 贵州省| 会宁县| 大城县| 武邑县| 城市| 铁岭市| 安徽省| 江阴市| 克什克腾旗| 宽甸| 柳江县| 东乌珠穆沁旗| 诸暨市|