本文共 14884 字,大约阅读时间需要 49 分钟。
在前面说明过使用Script数据源来获得web service数据源的做法,在实际操作中,发现虽然有BIRT的帮助文件,但同事对BIRT的Script数据源的使用还是不太理解,于是写出下文以便帮助使用BIRT的高级特性
熟悉了BIRT的Script数据源之后,你会感叹BIRT功能之强大,BIRT团队承诺在2.0中加入对数据库连接池的支持,但目前为止,我们还只能通过Script数据源来支持连接池。
为了能够自定义数据集合以及支持分页查询、多表查询、数据库连接池或者在DAO中使用Spring+Hibernate或从web Service获取数据等高级特性,我们需要使用BIRT的Script数据源来获得数据 下面通过一个示例说明如何使用BIRT的Script数据源来通过POJO获取数据:
注: 为了使例子不至于因为过于简单而无法说明情况(如同BIRT的Tutorial那样),在这里我使用了一个简单但完整的DAO层,可直接在项目中使用,同时也为避免过于复杂,本例中没有使用Spring+Hibernate或Web Service获得数据源,但从POJO中可很简单的将其改为SH组合或WS。 一、一个简单的数据库访问层
在开始我们浪费些时间来描述一下DAO层的几个类,以便后面在BIRT中使用它时有所了解。 首先在Eclipse中建立一个Tomcat项目,然后在src中建立一个com.bat.afp.DAOComm包用来封装一个非常简单的DAO类,如下:
其中DBUtil为数据库连接类(数据库为Oracle8),使用了DBCP作为数据库连接池,并使用XML文件(dbconfig.xml)来配置数据库连接池的信息
DBUtil代码如下: package com.bat.afp.DAOComm; import java.io.File; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.apache.commons.dbcp.DriverManagerConnectionFactory; import org.apache.commons.dbcp.PoolableConnectionFactory; import org.apache.commons.dbcp.PoolingDriver; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /* * * @author liuyf */ public class DBUtil { private static final Logger logger = Logger.getLogger(DBUtil. class ); private static DBUtil instance; private GenericObjectPool connectionPool; private static String dbUrl; private static String user; private static String password; private static int connNumber = 10 ; static { BasicConfigurator.configure(); try { readConfig(); } catch (DocumentException e) { e.printStackTrace(); } } private DBUtil() { try { initConnectionPool(); } catch (SQLException e) { e.printStackTrace(); } logger.debug( " DBUtil init " ); } /** * 读取配置文件 * * @throws DocumentException */ private static void readConfig() throws DocumentException { URL url = DBUtil. class .getClassLoader().getResource( " dbconfig.xml " ); File file = new File(url.getFile()); // File file = new File("dbconfig.xml"); SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); Element dbinfo = root.element( " dbinfo " ); dbUrl = dbinfo.elementText( " url " ); user = dbinfo.elementText( " user " ); password = dbinfo.elementText( " pwd " ); String numStr = dbinfo.elementText( " connNumber " ); if (numStr != null ) connNumber = Integer.parseInt(numStr); } public static DBUtil getInstance() { if (instance == null ) return instance = new DBUtil(); else return instance; } /* * * */ private void initConnectionPool() throws SQLException { DriverManager.registerDriver( new oracle.jdbc.OracleDriver()); connectionPool = new GenericObjectPool( null , connNumber); DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbUrl, user, password); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, connectionPool, null , null , false , true ); PoolingDriver driver = new PoolingDriver(); driver.registerPool( " afpdb " , connectionPool); } public Connection getConnection() throws SQLException { return DriverManager.getConnection( " jdbc:apache:commons:dbcp:afpdb " ); } } BIRT使用ScriptDataSet从POJO中获得数据(二) 2011-08-19 10:23
|
转载地址:http://potci.baihongyu.com/