在Tomcat服務(wù)器下使用連接池連接Oracle數(shù)據(jù)庫
更新時間:2014年01月23日 15:12:20 作者:
本文為大家介紹下在Tomcat服務(wù)器下使用連接池來連接數(shù)據(jù)庫的操作,下面有個不錯的示例,大家可以參考下
下面介紹在Tomcat服務(wù)器下使用連接池來連接數(shù)據(jù)庫的操作
一:修改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的數(shù)據(jù)庫驅(qū)動,加入到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("數(shù)據(jù)庫連接");
}
}catch(Exception e){
e.printStackTrace();
out.println("數(shù)據(jù)庫連接失敗");
}
%>
</body>
</html>
啟動Tomcat,這樣就可以訪問頁面。
一:修改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標簽之間加入如下代碼。
復(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ù)庫驅(qū)動,加入到Tomcat的lib包中。本項目中為:Ojdbc14.jar.
四:提供一個jsp頁面:
復(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ù)庫連接");
}
}catch(Exception e){
e.printStackTrace();
out.println("數(shù)據(jù)庫連接失敗");
}
%>
</body>
</html>
啟動Tomcat,這樣就可以訪問頁面。
您可能感興趣的文章:
- tomcat何時寫回響應(yīng)數(shù)據(jù)報的詳析
- Nginx + Tomcat實現(xiàn)請求動態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離詳解
- 解決Linux下Tomcat向MySQL插入數(shù)據(jù)中文亂碼問題
- 基于Tomcat 數(shù)據(jù)源的原理、配置、使用介紹
- Android實現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫)
- Tomcat 7-dbcp配置數(shù)據(jù)庫連接池詳解
- 使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
- 使用Get方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
- Tomcatc3p0配置jnid數(shù)據(jù)源2種實現(xiàn)方法解析
相關(guān)文章
Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細圖文教程)
這篇文章主要介紹了Oracle 10g 服務(wù)器端安裝預(yù)備步驟(詳細圖文教程),需要的朋友可以參考下2017-03-03Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案
在本篇文章里小編給大家整理的是關(guān)于Oracle ASM故障數(shù)據(jù)恢復(fù)解決方案以及相關(guān)知識點,有需要的朋友們參考下。2019-11-11