Java数据库连接池连接Oracle过程详解

网友投稿 720 2023-04-01

java数据库连接池连接Oracle过程详解

Java数据库连接池连接Oracle过程详解

第一步:导入tomcat\lib 下的一个tomcat-dbcp.jar包

第二步:在web\META-INF下新建一个context.xml文件,文件内容如下:

type="javax.sql.DataSource"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@192.168.1.3:1521:xe"

username="lwl"

password="123456bgOAgLb"

maxActive="5"

maxIdle="2"

maxWait="5000"/>

第三步:在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方法,就不是直接跟数据库连接了,是通过数据库连接池连接

type="javax.sql.DataSource"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@192.168.1.3:1521:xe"

username="lwl"

password="123456bgOAgLb"

maxActive="5"

maxIdle="2"

maxWait="5000"/>

第三步:在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方法,就不是直接跟数据库连接了,是通过数据库连接池连接

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java ArrayList如何实现生成不重复随机数
下一篇:mybatis Plus 多表联合查询的实现示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~