在Tomcat服務(wù)器下使用連接池連接Oracle數(shù)據(jù)庫(kù)
更新時(shí)間:2014年01月23日 15:12:20 作者:
本文為大家介紹下在Tomcat服務(wù)器下使用連接池來(lái)連接數(shù)據(jù)庫(kù)的操作,下面有個(gè)不錯(cuò)的示例,大家可以參考下
下面介紹在Tomcat服務(wù)器下使用連接池來(lái)連接數(shù)據(jù)庫(kù)的操作
一:修改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標(biāo)簽之間加入如下代碼。
<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的數(shù)據(jù)庫(kù)驅(qū)動(dòng),加入到Tomcat的lib包中。本項(xiàng)目中為:Ojdbc14.jar.
四:提供一個(gè)jsp頁(yè)面:
<%@ 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("數(shù)據(jù)庫(kù)連接");
}
}catch(Exception e){
e.printStackTrace();
out.println("數(shù)據(jù)庫(kù)連接失敗");
}
%>
</body>
</html>
啟動(dòng)Tomcat,這樣就可以訪問(wèn)頁(yè)面。
一:修改web.xml文件:
復(fù)制代碼 代碼如下:
<?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標(biāo)簽之間加入如下代碼。
復(fù)制代碼 代碼如下:
<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的數(shù)據(jù)庫(kù)驅(qū)動(dòng),加入到Tomcat的lib包中。本項(xiàng)目中為:Ojdbc14.jar.
四:提供一個(gè)jsp頁(yè)面:
復(fù)制代碼 代碼如下:
<%@ 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("數(shù)據(jù)庫(kù)連接");
}
}catch(Exception e){
e.printStackTrace();
out.println("數(shù)據(jù)庫(kù)連接失敗");
}
%>
</body>
</html>
啟動(dòng)Tomcat,這樣就可以訪問(wèn)頁(yè)面。
您可能感興趣的文章:
- tomcat何時(shí)寫回響應(yīng)數(shù)據(jù)報(bào)的詳析
- Nginx + Tomcat實(shí)現(xiàn)請(qǐng)求動(dòng)態(tài)數(shù)據(jù)和請(qǐng)求靜態(tài)資源的分離詳解
- 解決Linux下Tomcat向MySQL插入數(shù)據(jù)中文亂碼問(wèn)題
- 基于Tomcat 數(shù)據(jù)源的原理、配置、使用介紹
- Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫(kù))
- Tomcat 7-dbcp配置數(shù)據(jù)庫(kù)連接池詳解
- 使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
- 使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
- Tomcatc3p0配置jnid數(shù)據(jù)源2種實(shí)現(xiàn)方法解析
相關(guān)文章
oracle表空間中空表統(tǒng)計(jì)方法示例介紹
這篇文章主要介紹了oracle表空間中空表統(tǒng)計(jì)方法,需要的朋友可以參考下2014-04-04Oracle單行子查詢返回多行結(jié)果的問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于Oracle中單行子查詢返回多行結(jié)果的問(wèn)題解決的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用oracle具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細(xì)圖文教程)
這篇文章主要介紹了Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細(xì)圖文教程),需要的朋友可以參考下2017-03-03oracle如何恢復(fù)被覆蓋的存儲(chǔ)過(guò)程
如果你不小心覆蓋了之前的存儲(chǔ)過(guò)程,那得趕緊閃回,時(shí)長(zhǎng)越長(zhǎng)閃回的可能性越小,下面為大家介紹下恢復(fù)原理2014-05-05Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案
在本篇文章里小編給大家整理的是關(guān)于Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案以及相關(guān)知識(shí)點(diǎn),有需要的朋友們參考下。2019-11-11oracle 發(fā)送郵件 實(shí)現(xiàn)方法
oracle 發(fā)送郵件 實(shí)現(xiàn)方法2009-05-05