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.客户 ...
随机推荐
- DedeCMS模板中用彩色tag做彩色关键词
DedeCMS模板中用彩色tag做彩色关键词,下面分享一下吧!修改方法: 1.在/include/common.func.php 中加入如下函数: function getTagStyle() { $ ...
- Ext js框架模拟Windows桌面菜单管理模板
一款超炫的后台,Ext模拟Windows桌面,Ext经典浅蓝风格,功能非常强大,包括最大化.最小化.状态栏.桌面图标等,不过需要非常懂Ext脚本的才可驾驭它. 1.图片 2. [代码][HTML] ...
- 非常精彩的Silverlight 2控件样式
概述 大家是否觉的现在Silverlight 2提供的默认的控件不能满足自己的要求?好在Silverlight的控件可以运用皮肤,微软Silverlight控件的设计者的主管Corrina开发了几套非 ...
- 第一次通过AVD Manager创建了一个虚拟设备,但是在Android Studio运行程序时却无设备可选
第一次通过AVD Manager创建了一个虚拟设备,但是在Android Studio运行程序时却无设备可选 原因是adb.exe未运行起来 至于adb.exe未正常运行起来的原因多半是5037端口被 ...
- 获取cookie值
function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (do ...
- centos7 编译安装新版LNMP环境
centos7 编译安装新版LNMP环境 环境版本如下: 1.系统环境:Centos 7 x86_64 2.NGINX:nginx-1.11.3.tar.gz 3.数据库:mariadb-10.0.2 ...
- 压缩&&解压
压缩与解压缩: ############################################################# tar xvf wordpress.tar ## ...
- Win7系统打开服务管理界面的几种方法汇总
转自:https://www.jb51.net/os/windows/318465.html Win7服务管理包含了计算机操作系统和应用程序提供的所有服务,但是这么多服务并非总是用户所需的.比如打印机 ...
- C# 利用Aspose.Cells .dll将本地excel文档转化成pdf(完美破解版 无水印 无中文乱码)
Aspose.Cells .dll下载 http://pan.baidu.com/s/1slRENLF并引用 C#代码 using System; using System.Collections. ...
- VS2008 视图资源.rc无法加载的问题及解决方法
VS2008 视图资源.rc无法加载 1.首先先把vs关闭,然后执行 开始>>所有程序>>Mircosoft visual studio 2008>>visual ...