国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

在Tomcat服務(wù)器下使用連接池連接Oracle數(shù)據(jù)庫

2020-07-26 14:21:38
字體:
供稿:網(wǎng)友
下面介紹在Tomcat服務(wù)器下使用連接池來連接數(shù)據(jù)庫的操作

一:修改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ù)庫驅(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,這樣就可以訪問頁面。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洱源县| 宜兴市| 武义县| 清苑县| 宁都县| 汝州市| 平江县| 永修县| 潍坊市| 贵阳市| 嵩明县| 安仁县| 江山市| 当雄县| 年辖:市辖区| 天等县| 棋牌| 泸定县| 彭水| 呼图壁县| 绥宁县| 新乡县| 绥德县| 岢岚县| 永德县| 叙永县| 茌平县| 瑞昌市| 上思县| 濮阳县| 成安县| 兴义市| 纳雍县| 阿巴嘎旗| 夏河县| 兴宁市| 宁陵县| 上犹县| 吉木乃县| 永德县| 迭部县|