Java數據庫連接池連接Oracle過程詳解
第一步:導入tomcatlib 下的一個tomcat-dbcp.jar包
第二步:在webMETA-INF下新建一個context.xml文件,文件內容如下:
<?xml version='1.0' encoding='UTF-8'?><Context><Resource name='jdbc/lwl' auth='Container'type='javax.sql.DataSource'driverClassName='oracle.jdbc.driver.OracleDriver'url='jdbc:oracle:thin:@192.168.1.3:1521:xe'username='lwl'password='123456'maxActive='5'maxIdle='2'maxWait='5000'/></Context>
第三步:在Dao里的getConn里寫:
private static Connection getConn() {Connection conn = null;try {Context ctx = new InitialContext();DataSource ds = (DataSource) ctx.lookup('java:comp/env/jdbc/lwl');conn = ds.getConnection();} catch (Exception e) {System.err.println('error:' + e.getMessage());}return conn;}
此時后面再調用getConn方法,就不是直接跟數據庫連接了,是通過數據庫連接池連接
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: