一、Tomcat6.0中配置数据源
1.在Tomcat根目录/conf/Catalina/localhost目录下新建一个XML文件,文件名称跟工程名称一致.文件中的内容如下:
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中的
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
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是否自动提交
不懂得 等高手出现
tomcat的文档中有说明,参考