第一步:导入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="123456"
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方法,就不是直接跟数据库连接了,是通过数据库连接池连接
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持龙方网络。