tomcat6配置数据库连接池
程序员文章站
2022-04-22 09:40:01
...
1.将数据库的jar包放到工程的lib下(jar包见附件)
2.在tomcat的/conf/context.xml文件中添加(一些参数需要修改)
<Resource name="jdbc/oracle" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
username="dtm" password="dtm" maxActive="50" maxIdle="20" maxWait="10000" />
3.测试代码
public class DBConn {
private static Connection conn;
public static Connection getConn(String name){
try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/oracle");
conn = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
PS:红色字体部分要相同