第一步:導入tomcat\lib 下的一個tomcat-dbcp.jar包
第二步:在web\META-INF下新建一個context.xml文件,文件內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < Context > < Resource name = "jdbc/lwl" auth = "Container" type = "javax.sql.DataSource" url = "jdbc:oracle:thin:@192.168.1.3:1521:xe" username = "lwl" password = "123456" maxActive = "5" maxIdle = "2" maxWait = "5000" /> </ Context > |
第三步:在Dao里的getConn里寫:
1
2
3
4
5
6
7
8
9
10
11
|
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方法,就不是直接跟數據庫連接了,是通過數據庫連接池連接
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/lwl80/p/13548009.html