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

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

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

           

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

           

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

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

           

          注:

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

          一、一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)層

           

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

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

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

           

          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 閱讀(6944) 評(píng)論(7)  編輯  收藏 所屬分類(lèi): Eclipse Tech

          評(píng)論

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

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

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

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

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

          2005-09-10 21:28 | fisher

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

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

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

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

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

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

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

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

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

          能不能給一個(gè)birt集成spring + struts2 + hibernate 的例子啊!!!
          3Q
          2008-11-02 18:29 | datalong
          主站蜘蛛池模板: 那曲县| 土默特左旗| 中卫市| 慈溪市| 舒城县| 成都市| 双柏县| 内乡县| 西昌市| 平顺县| 靖西县| 清徐县| 永新县| 榕江县| 仙桃市| 留坝县| 大同市| 河间市| 九江县| 高密市| 响水县| 松江区| 苏尼特左旗| 茌平县| 吕梁市| 莫力| 天柱县| 黑龙江省| 徐水县| 嘉义市| 卢氏县| 股票| 册亨县| 翼城县| 广饶县| 岱山县| 读书| 兰西县| 安阳市| 同德县| 神木县|