开发环境: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. java 连接飞信API

    通过java连接飞信api给自己的好友(包括自己)发送飞信内容.如果对方的手机号非你的飞信好友则不能发送.​​1. [代码]飞信发送类     package per.artisan.fetion; ...

  2. phpstudy开启时mysql不开启时红色灯

    1.本身为开启 2.mysql服务没有启动

  3. HDUoj4857逃生 拓扑排序

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  4. 关于 .dyib 文件

    .dylib 意味着这是一个动态链接库. libz.dylib 是提供zip压缩.解压缩的库. 库的接口请 #import "zlib.h"

  5. 通过配置Mysql参数提高写入速度(整理)

    1) innodb_buffer_pool_size 如果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于buffer size更敏感.MySIAM可能对于大数据量使用默 ...

  6. ARC和MRC混合使用

    在一些项目中尤其是做迭代的项目经常会出现MRC的项目,但是我们习惯了ARC环境,反之也是一样.这是我们不必去修改代码去掉release之类的,按照如下方案去做就可以了. 项目 -> Build ...

  7. python 子类调用父类成员的方法

    1.直接写类名调用: parent_class.parent_attribute(self) class Animal(): def __init__(self, name): self.name = ...

  8. weui button的使用

    1.迷你按钮的使用 <a href="javascript:;" class="weui-btn weui-btn_primary weui-btn_mini&qu ...

  9. 黑客攻防技术宝典web实战篇:攻击访问控制习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 一个应用程序可能通过使用 HTTP Referer 消息头实施访问控制,但它的正常行为并没 ...

  10. C++十进制到任意进制

    #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #in ...