在Tomcat中配备DataSource JNDI
在Tomcat中配置DataSource JNDI
在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");