Tomcat配置Oracle数据源
开发环境:Eclipse luna、tomcat 7、Oracle
配置Oracle datasource步骤
第一步:打开tomcat目录下的 context.xml 文件,添加 Resource 标签内容
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context> <!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
--> <!-- oracle datasource -->
<Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdel="30" maxWait="1000"
username="scott" password="goodluck" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin://localhost:1521/orcl?allowMultiQueries=true"/> </Context>
第二步:编辑工程下面的 web.xml文件,插入 resource-ref 标签内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>web01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/orcl</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> </web-app>
第三步:数据库连接程序中添加三行代码
Context sourceCtx = new InitialContext();
DataSource ds = (DataSource) sourceCtx.lookup("java:comp/env/jdbc/orcl");
conn = ds.getConnection();
第四步:测试程序
重构 Java Web MVC实例 数据库连接程序
package com.test.dao; import java.sql.Connection;
import java.sql.SQLException; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource; public class BaseDao {
Connection conn = null; public BaseDao() {
} public Connection dbConnection() throws SQLException{
try {
Context sourceCtx = new InitialContext();
DataSource ds = (DataSource) sourceCtx.lookup("java:comp/env/jdbc/orcl");
conn = ds.getConnection();
System.out.println("----> Connection Success!");
} catch (NamingException e) {
e.printStackTrace();
} return conn;
} public void dbDisconnection() throws SQLException{
conn.close();
System.out.println("----> Connection End!");
}
}
重构调用处的代码
package com.test.service; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import com.test.bean.EmpBean;
import com.test.dao.BaseDao; public class EmpService {
int idx = 1; public ArrayList<EmpBean> getEmpList(EmpBean eb){ Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null; ArrayList<EmpBean> empList = new ArrayList<EmpBean>(); BaseDao baseDao = new BaseDao();
try {
conn = baseDao.dbConnection();
} catch (SQLException e1) {
e1.printStackTrace();
} // 3. 执行SQL语句
StringBuffer sqlBf = new StringBuffer();
sqlBf.setLength(0); sqlBf.append("SELECT EMPNO \n");
sqlBf.append(" , ENAME \n");
sqlBf.append(" , JOB \n");
sqlBf.append(" , MGR \n");
sqlBf.append(" , TO_CHAR(HIREDATE, 'YYYYMMDD') HIREDATE \n");
sqlBf.append(" , SAL \n");
sqlBf.append(" , COMM \n");
sqlBf.append(" , DEPTNO \n");
sqlBf.append("FROM EMP \n");
sqlBf.append("WHERE ENAME LIKE UPPER(?) || '%' \n"); System.out.println(sqlBf.toString()); try {
pstmt = conn.prepareStatement(sqlBf.toString());
idx = 1;
pstmt.setString(idx++, eb.getEname()); // 4. 获取结果集
rs = pstmt.executeQuery();
while (rs.next()) {
EmpBean emp = new EmpBean(); emp.setEmpNo(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("mgr"));
emp.setHiredate(rs.getString("hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno")); empList.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
} try {
baseDao.dbDisconnection();
} catch (SQLException e) {
e.printStackTrace();
}
return empList;
}
}
第五步:重新启动web工程测试已有程序是不是访问正常
参考文章:http://pengjianbo1.iteye.com/blog/503326
Tomcat配置Oracle数据源的更多相关文章
- weblogic配置oracle数据源
在weblogic配置oracle数据源还是挺简单的,网上也有很多关于这方面的文章,写给自己也写给能够得到帮助的人吧.weblogic新建域那些的就不说了哈.点击startWebLogic文件,会弹出 ...
- Tomcat配置JNDI数据源
经过3个多小时的努力,配置JNDI数据源(主要是通过DBCP连接池)终于搞定-还是Tomcat官方的说明好,不过全是英文的,大概还看得懂.百度上那么花花绿绿的太多了,一个也没成功!...本例使用的数据 ...
- 【转】在Tomcat配置JNDI数据源的三种方式
在我过去工作的过程中,开发用服务器一般都是Tomcat 数据源的配置往往都是在applicationContext.xml中配置一个dataSource的bean 然后在部署时再修改JNDI配置 我猜 ...
- Tomcat配置JNDI数据源的三种方式-转-http://blog.51cto.com/xficc/1564691
第一种,单个应用独享数据源 就一步,找到Tomcat的server.xml找到工程的Context节点,添加一个私有数据源 Xml代码 <Context docBase="WebA ...
- 在Tomcat配置JNDI数据源的三种方式
最近使用到了在tomcat下配置数据源的内容,在这里转载一篇文章记录下 转载自: http://blog.csdn.net/dyllove98/article/details/7706218 在我过去 ...
- 【Tomcat】Tomcat 配置JNDI数据源(三)
数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connec ...
- BIEE11G配置Oracle数据源
注:数据库发生变化只需要修改视图层 两种方式: (1) 在BIEE自带的Oracle客户端目录下的tnsname.ora文件中配置 把E:\app\Administrator\produc ...
- springboot+jndi+tomcat配置多数据源
1.在application.properties中,添加jndi配置,如下图 2.新建dataSourceConfig类 3.dataSourceConfig类详细代码,这里只贴出其中一个,多个数据 ...
- oracle配置odbc数据源
今天配置oracle数据源心得: 1.需安装oracle客户端,若校验报错,将杀毒软件全部退出之后再重新安装: 2.安装完成后,运行odbcad32(64位),在odbc界面可找到相应驱动: 3.客户 ...
随机推荐
- YTU 2578: 分数减法——结构体
2578: 分数减法--结构体 时间限制: 1 Sec 内存限制: 128 MB 提交: 522 解决: 399 题目描述 分数可以看成是由字符'/'分割两个整数构成,可以用结构体类型表示.请用结 ...
- JSON详解+ C# String.Format格式说明+ C# ListView用法详解 很完整
JSON详解 C# String.Format格式说明 C# ListView用法详解 很完整
- poj 2185 Milking Grid(next数组求最小循环节)
题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...
- angularjs 获得当前元素属性
先用 console.log(this)查看下当前被点击元素的 this 属性,然后可以看见里面有个$index属性,该属性指向的就是DOM元素列表中当前被点击的那个DOM的下标,只需要使用this. ...
- 一些好用的Linux命令组合
1.删除0字节文件 代码如下: find -type f -size 0 -exec rm -rf {} \; 2.查看进程按内存从大到小排列 代码如下: ps -e -o "%C : %p ...
- NYOJ8——一种排序
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数: ...
- PTA 模拟,【放着一定要写哈哈哈哈哈】(据说用string哟)
实现一种简单原始的文件相似度计算,即以两文件的公共词汇占总词汇的比例来定义相似度.为简化问题,这里不考虑中文(因为分词太难了),只考虑长度不小于3.且不超过10的英文单词,长度超过10的只考虑前10个 ...
- N的阶乘HDOJ1042
我记得有一份代码是非常有技巧的,然而这一份就是很死板-每次跑50000,因为10000的阶乘最多才50000位,这样肯定就过了 #include<cstdio> #include<s ...
- python 之 random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...
- YCOJ-DFS
DFS搜索是搜索中的一种,即深度优先搜索(Depth First Search),其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 图示: 如图,这是邻接矩阵,我 ...