在前面说明过使用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代码如下:
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, null, null, false, true);
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
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 liuyf21
*/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
static38

{39
BasicConfigurator.configure();40
try41

{42
readConfig();43
}44
catch (DocumentException e)45

{46
e.printStackTrace();47
}48
}49

50
private DBUtil()51

{52
try53

{54
initConnectionPool();55
}56
catch (SQLException e)57

{58
e.printStackTrace();59
}60
logger.debug("DBUtil init
");61
}62

63

/**//**64
* 读取配置文件65
* 66
* @throws DocumentException67
*/68
private static void readConfig() throws DocumentException69

{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
else90
return instance;91
}92

93

/**//**94
* 95
*/96
private void initConnectionPool() throws SQLException97

{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

108
public Connection getConnection() throws SQLException109

{110
return DriverManager.getConnection("jdbc:apache:commons:dbcp:afpdb");111
}112
}113

发表评论
- 浏览: 28240 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
链接
最新评论
-
买桔子 - 管理小故事
这类管理小故事都是吓扯淡,你碰上个不解风情的老板试试,类似故事中的升职者,人家还 ...
-- by liusu -
买桔子 - 管理小故事
说实话,我很不喜欢许三多 引用一句网友的话:许三多只有在军队里才有可能成功。 ...
-- by shevliu -
买桔子 - 管理小故事
不喜欢三多。我引三多这个故事 就是说明一下对不同的人才要有不同的引导办法。这个职 ...
-- by hysoft -
买桔子 - 管理小故事
班长只是让他去看书,并没有说需要看多少书,也没有说教他看哪一段,作为他们连队来说 ...
-- by flyingbug -
买桔子 - 管理小故事
我所知道的,比如班长叫三多去买橘子三多一定会按那个职员(要求升职者)所做。 你 ...
-- by hysoft






评论排行榜