随笔 - 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 阅读(6891) 评论(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

只有注册用户登录后才能发表评论。


网站导航: