1. 配置tomcat下的conf下的context.xml文件,在之間添加連接池配置:
<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" 驅(qū)動(dòng)類名 url=" jdbc:oracle:thin:@host:port:databse" 連接字符串 username=" user " password="password" maxActive="100" 表示最大活動(dòng)連接數(shù) maxIdle="30" 表示最大保留連接數(shù) maxWait="10000" /> 表示最大等待時(shí)間 2. 配置你的應(yīng)用下的web.xml中的之間加入 <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/oracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 3.把連接數(shù)據(jù)庫的第三方驅(qū)動(dòng)放到common/lib下面就ok了 4.import javax.naming.InitialContext; import javax.sql.DataSource; public class Test1 { public static Connection getConn() { Connection conn = null; Context initContext; try { initContext = new InitialContext(); DataSource ds = (DataSource)initContext.lookup("java:comp/env/jdbc/oracle"); conn = ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conn; } 5.private static final ResourceBundle dataResources =ResourceBundle.getBundle("com.langchao.se.properties.database",Locale.getDefault()); DataSourceFactory dsf = DataSourceFactory.create(); Connection conn = dsf.getConnection(dataSource); |
|