日期:2014-05-16 浏览次数:20448 次
<!-- appName 为项目名 --!> <Context path="/appName" docBase="appName" auth="Container"> <Resource name="jdbc/oracleDS" scope="Shareable" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:orcl" driverClassName="oracle.jdbc.driver.OracleDriver" username="scott" password="tiger" maxWait="3000" maxIdle="100" maxActive="10" /> </Context>
<resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/oracleDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
import java.sql.Connection; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class DBUtil { public Connection getConnection() throws Exception { Context context = new InitialContext(); // 获取数据源 DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/oracleDS"); // 获取数据库连接 Connection conn = ds.getConnection(); if (conn != null && !conn.isClosed()) { return conn; } else { return null; } } }