在Tomcat中配置DataSource及调用需要三步:
1.server.xml中加入以下片段:
<Resource name="jdbc/oracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ora1"
username="bond" password="bond" maxActive="20" maxIdle="10"
maxWait="-1"/>
2.在项目web.xml中加入:
<resource-ref> <res-ref-name>jdbc/oracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
3.程序中调用
@Resource(mappedName="jdbc/oracle")
private DataSource dataSource;
或:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
本文介绍如何在Tomcat中配置连接Oracle数据库的DataSource。通过在server.xml和web.xml中进行必要的配置,并展示如何在Java程序中获取DataSource实例。

4783

被折叠的 条评论
为什么被折叠?



