tomcat下localhost里面怎么配置数据源

2025-03-22 19:42:13
推荐回答(3个)
回答1:

一、Tomcat6.0中配置数据源
1.在Tomcat根目录/conf/Catalina/localhost目录下新建一个XML文件,文件名称跟工程名称一致.文件中的内容如下:



factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.2.250:1521:hmisb"
username="mtms" password="mtms"
validationQuery="select 1 from dual"
maxIdle="100" maxActive="500" maxWait="1000" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true"/>

2.在tomcat的conf/context.xml中的标签中添加一个,内容如下:
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="500" maxWait="10000"
username="oa" password="oa" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@172.16.60.30:1521:HMIS" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true" />
然后在web.xml中添加引用(tomcat5.5以上可以不用添加这一段)

DB Connection
jdbc/oa
javax.sql.DataSource
Container


2.1.获取连接对象
public class ConnectionPool{
public static Connection getConn()throws Exception{
//初始化上下文
Context initContext=getInitContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource dataSource=(DataSource)envContext.lookup("jdbc/oa");
//获取连接对象
return ds.getConnection();
}
}

docBase是指Web工程所在的位置,path是工程的名称, name是指JNDI的名称,type是数据源的类型,driverClassName是驱动名称,url是驱动的连接字符串
username是指数据库的用户名,password是指数据库的密码,defaultAutoCommit是否自动提交

回答2:

不懂得 等高手出现

回答3:

tomcat的文档中有说明,参考