[BIRT]-[Tutorial]-使用ScriptDataSet从POJO中获得数据(一)

[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代码如下:

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

 22 public   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

你可能感兴趣的:([BIRT]-[Tutorial]-使用ScriptDataSet从POJO中获得数据(一))