在前面说明过使用Script数据源来获得web service数据源的做法,在实际操作中,发现虽然有BIRT的帮助文件,但同事对BIRT的Script数据源的使用还是不太理解,于是写出下文以便帮助使用BIRT的高级特性
<o:p> </o:p>
熟悉了BIRT的Script数据源之后,你会感叹BIRT功能之强大,BIRT团队承诺在2.0中加入对数据库连接池的支持,但目前为止,我们还只能通过Script数据源来支持连接池。
<o:p> </o:p>
为了能够自定义数据集合以及支持分页查询、多表查询、数据库连接池或者在DAO中使用Spring+Hibernate或从web Service获取数据等高级特性,我们需要使用BIRT的Script数据源来获得数据
下面通过一个示例说明如何使用BIRT的Script数据源来通过POJO获取数据:
<o:p> </o:p>
注:<o:p></o:p>
为了使例子不至于因为过于简单而无法说明情况(如同BIRT的Tutorial那样),在这里我使用了一个简单但完整的DAO层,可直接在项目中使用,同时也为避免过于复杂,本例中没有使用Spring+Hibernate或Web Service获得数据源,但从POJO中可很简单的将其改为SH组合或WS。
<o:p>
一、一个简单的数据库访问层
<o:p> </o:p>
在开始我们浪费些时间来描述一下DAO层的几个类,以便后面在BIRT中使用它时有所了解。
首先在Eclipse中建立一个Tomcat项目,然后在src中建立一个com.bat.afp.DAOComm包用来封装一个非常简单的DAO类,如下:
<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype>
![o_1.jpg](/images/blogjava_net/flyingbug/2501/o_1.jpg)
其中DBUtil为数据库连接类(数据库为Oracle8),使用了DBCP作为数据库连接池,并使用XML文件(dbconfig.xml)来配置数据库连接池的信息
<o:p> </o:p>
DBUtil代码如下:
1
package com.bat.afp.DAOComm;
2
![None.gif](/Images/OutliningIndicators/None.gif)
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
![None.gif](/Images/OutliningIndicators/None.gif)
19
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/**
20
* @author liuyf
21
*/
22
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
class
DBUtil
{
23![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
private static final Logger logger = Logger.getLogger(DBUtil.class);
25![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
private static DBUtil instance;
27![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
private GenericObjectPool connectionPool;
29![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
private static String dbUrl;
31![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
private static String user;
33![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
private static String password;
35![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
private static int connNumber = 10;
37
static
38![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
BasicConfigurator.configure();
40
try
41![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
readConfig();
43
}
44
catch (DocumentException e)
45![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
e.printStackTrace();
47
}
48
}
49![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
50
private DBUtil()
51![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
try
53![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
initConnectionPool();
55
}
56
catch (SQLException e)
57![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
e.printStackTrace();
59
}
60
logger.debug("DBUtil init
");
61
}
62![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
63![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**
64
* 读取配置文件
65
*
66
* @throws DocumentException
67
*/
68
private static void readConfig() throws DocumentException
69![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
85
public static DBUtil getInstance()
86![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
if (instance == null)
88
return instance = new DBUtil();
89
else
90
return instance;
91
}
92![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
93![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**
94
*
95
*/
96
private void initConnectionPool() throws SQLException
97![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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, null, null, false, true);
104
PoolingDriver driver = new PoolingDriver();
105
driver.registerPool("afpdb", connectionPool);
106
}
107![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
108
public Connection getConnection() throws SQLException
109![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
return DriverManager.getConnection("jdbc:apache:commons:dbcp:afpdb");
111
}
112
}
113
</o:p>