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.客户 ...
随机推荐
- java前端学习步骤
前端说的比较好的知乎:https://www.zhihu.com/question/22759296 网站开发绝杀技:https://ke.qq.com/course/20945?from=qqcha ...
- CodeForces161D: Distance in Tree(树分治)
A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a ...
- 【转】设置cocos2dx 屏幕分辨率
[转载连接:]http://www.cnblogs.com/onlycxue/p/3500026.html 做手机上的软件首先要考虑的就是屏幕分辨率怎么解决.coco2dx已经有了很好的解决方法. 用 ...
- JAVA interrupte中断线程的真正用途
Java线程之中,一个线程的生命周期分为:初始.就绪.运行.阻塞以及结束.当然,其中也可以有四种状态,初始.就绪.运行以及结束. 一般而言,可能有三种原因引起阻塞:等待阻塞.同步阻塞以及其他阻塞(睡眠 ...
- 任务34:Cookie-based认证实现
任务34:Cookie-based认证实现 用mvc来实现以下Cookie-Base的认证和授权的方式 新建一个web MVC的项目 在我的电脑的路径:D:\MyDemos\jesse Ctrl+鼠标 ...
- E20180410-sl
category n. 类型,部门,种类,类别,类目; [逻,哲] 范畴; 体重等级;
- bzoj 3232: 圈地游戏【分数规划+最小割】
数组开小导致TTTTTLE-- 是分数规划,设sm为所有格子价值和,二分出mid之后,用最小割来判断,也就是判断sm-dinic()>=0 这个最小割比较像最大权闭合子图,建图是s像所有点连流量 ...
- python中用代码实现99乘法表
第一种:使用for遍历循环嵌套 ,): ,x+): print("%s*%s=%s" % (y,x,x*y),end=" ") print("&quo ...
- ubuntu 18 安装virtulenv以及virtualenvwrapper
转自: https://www.jianshu.com/p/06533f19c4ad 首先安装pip,如果用的python3版本要安装pip3(自行google) sudo apt install v ...
- NowCoder小定律
题目:https://www.nowcoder.com/pat/2/problem/259 #include <cstdio> #include <cstring> #incl ...