在Tomcat服务器下使用连接池连接Oracle数据库

所属分类: 数据库 / oracle 阅读数: 612
收藏 0 赞 0 分享
下面介绍在Tomcat服务器下使用连接池来连接数据库的操作

一:修改web.xml文件:
复制代码 代码如下:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>project</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>DBConnection</description>
<res-ref-name>siniteksirm</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

在web-app之间插入<resource-ref>这段代码。指定要是用的Resource名称。

二:修改tomcat下的context.xml文件:

在Context标签之间加入如下代码。
复制代码 代码如下:

<Resource name="siniteksirm" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.196:1521:orcl"
username="paxt"
password="paxt"
maxActive="20"
maxIdle="10"
maxWait="-1"
testOnBorrow="true"
validationQuery="select 1 from dual"/>

三:选择Oracle的数据库驱动,加入到Tomcat的lib包中。本项目中为:Ojdbc14.jar.

四:提供一个jsp页面:
复制代码 代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.DataSource" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
DataSource ds = null;
try{
Context context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/siniteksirm");
Connection conn = ds.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from sdc_fundbase where rownum <= 2");
ResultSet rs = pst.executeQuery();
while(rs.next()){
out.println(rs.getString("fund4"));
out.println("<br/>");
}
if(ds != null){
out.println("数据库连接");
}
}catch(Exception e){
e.printStackTrace();
out.println("数据库连接失败");
}
%>
</body>
</html>

启动Tomcat,这样就可以访问页面。
更多精彩内容其他人还在看

oracle存储过程中return和exit区别概述及测试

至于return和exit在oracle存储过程中的应用,有些新手朋友们还是比较容易混淆的,本文将针对这两个关键字进行详细对比下,感兴趣的你可以参考下,希望可以帮助到你
收藏 0 赞 0 分享

oracle查看当前日期是第几个星期的方法

oracle查看当前日期是第几个星期方法的代码段,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle删除已存在的表的实例

查询系统表,判断表是否存在,存在则直接删除
收藏 0 赞 0 分享

oracle中文乱码解决的办法

oracle中文乱码解决的办法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle中在pl/sql developer修改表的2种方法

Oracle中在pl/sql developer修改表的2种方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle 创建表空间步骤代码

oracle 创建表空间步骤代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle 查看表空间的大小及使用情况sql语句

表空间使用情况包括:查看表空间的名称及大小/查看表空间物理文件的名称及大小/查看回滚段名称及大小等等感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle Form中COMMIT的概述及使用技巧

针对form上面的数据变动提交到后台数据库,同时数据库提交数据,接下来将详细介绍下Form中COMMIT的使用,感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle跨数据库查询并插入实现原理及代码

需要从一个数据库中的表GIS_WEICHAI_DATA_1S中的数据导入到另个一数据库的表GIS_WEICHAI_DATA_1S中,接下来为你讲解跨数据库查询并插入需要的朋友可以参考下
收藏 0 赞 0 分享

Oracle 存储过程发送邮件实例学习

接下来将介绍下如何使用存储过程发送邮件这一案例实现,感兴趣的你可以参考下本文或许对你有所帮助
收藏 0 赞 0 分享
查看更多