开发环境: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数据源的更多相关文章

  1. weblogic配置oracle数据源

    在weblogic配置oracle数据源还是挺简单的,网上也有很多关于这方面的文章,写给自己也写给能够得到帮助的人吧.weblogic新建域那些的就不说了哈.点击startWebLogic文件,会弹出 ...

  2. Tomcat配置JNDI数据源

    经过3个多小时的努力,配置JNDI数据源(主要是通过DBCP连接池)终于搞定-还是Tomcat官方的说明好,不过全是英文的,大概还看得懂.百度上那么花花绿绿的太多了,一个也没成功!...本例使用的数据 ...

  3. 【转】在Tomcat配置JNDI数据源的三种方式

    在我过去工作的过程中,开发用服务器一般都是Tomcat 数据源的配置往往都是在applicationContext.xml中配置一个dataSource的bean 然后在部署时再修改JNDI配置 我猜 ...

  4. Tomcat配置JNDI数据源的三种方式-转-http://blog.51cto.com/xficc/1564691

    第一种,单个应用独享数据源 就一步,找到Tomcat的server.xml找到工程的Context节点,添加一个私有数据源 Xml代码   <Context docBase="WebA ...

  5. 在Tomcat配置JNDI数据源的三种方式

    最近使用到了在tomcat下配置数据源的内容,在这里转载一篇文章记录下 转载自: http://blog.csdn.net/dyllove98/article/details/7706218 在我过去 ...

  6. 【Tomcat】Tomcat 配置JNDI数据源(三)

    数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connec ...

  7. BIEE11G配置Oracle数据源

    注:数据库发生变化只需要修改视图层 两种方式: (1)       在BIEE自带的Oracle客户端目录下的tnsname.ora文件中配置 把E:\app\Administrator\produc ...

  8. springboot+jndi+tomcat配置多数据源

    1.在application.properties中,添加jndi配置,如下图 2.新建dataSourceConfig类 3.dataSourceConfig类详细代码,这里只贴出其中一个,多个数据 ...

  9. oracle配置odbc数据源

    今天配置oracle数据源心得: 1.需安装oracle客户端,若校验报错,将杀毒软件全部退出之后再重新安装: 2.安装完成后,运行odbcad32(64位),在odbc界面可找到相应驱动: 3.客户 ...

随机推荐

  1. Go——godoc命令简介

    前言 godoc的一些简记 命令 godoc的列表 | godoc的chm下载 查看godoc的所有命令 `$ godoc -h` usage: godoc -http=localhost:6060 ...

  2. netty codec部分剖析

    针对netty 3.2进行剖析 今天用到了netty的encoder和decoder(coder其本质还是handler),特剖析一个netty提供的coder,从而选择或者实现我自己的coder. ...

  3. Python里的一些注释规范

    写代码注释是一件很重要的事情,如果你写的一段函数给别人调用那么往往都需要配上一些基本的注释.写好代码可以让别人容易阅读你的代码.试想一 下:如果你在github上面找到一段你想要的代码,这段代码有20 ...

  4. Laravel中常见的错误与解决方法小结

    一.报错: 「Can't swap PDO instance while within transaction」 通过查询 Laravel 源代码,可以确认异常是在 setPdo 方法中抛出的: ? ...

  5. CS231n 2016 通关 第二章-KNN 作业分析

    KNN作业要求: 1.掌握KNN算法原理 2.实现具体K值的KNN算法 3.实现对K值的交叉验证 1.KNN原理见上一小节 2.实现KNN 过程分两步: 1.计算测试集与训练集的距离 2.通过比较la ...

  6. 如何从kernel源码中查出版本号(转载)

    转载:http://m.android.tgbus.com/tgmobile/arc/174624.shtml 目前查版本号的方法都是在编译以后从rootfs里看的,难道从源码就看不到,一定要编译以后 ...

  7. js遍历ajax回调函数返回值中的object对象

    function printObject(obj) {      //obj = {"cid":"C0","ctext":"区县& ...

  8. Codeforces - 773A - Success Rate - 二分 - 简单数论

    https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<= ...

  9. hbase表结构 + hbase集群架构及表存储机制

    本博文的主要内容有    .hbase读取数据过程 .HBase表结构 .附带PPT http://hbase.apache.org/ 读写的时候,就需要用hbase了,换句话说,就是读写的时候.需要 ...

  10. hdu1879 继续畅通工程 基础最小生成树

    #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> u ...