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

首頁 > 學院 > 開發設計 > 正文

JNDI連接池連接Oracle數據庫

2019-11-15 00:17:10
字體:
來源:轉載
供稿:網友
JNDI連接池連接Oracle數據庫

今天做了一個評論的小功能,要求用JNDI連接池連接Oracle數據庫,以前只是測試了是否連接的上,現在沒想到一個JNDI連接池連接Oracle數據庫,糾結了好久,原來都是Oracle數據庫的問題,這是過失。下面介紹一下JNDI連接池連接Oracle數據庫。

JNDI介紹什么是JNDI?JNDI(java Naming and Directory Interface,Java命名和目錄接口)是一組在Java應用中訪問命名和目錄服務的API通過名稱將資源與服務進行關聯什么是連接池技術?連接池連接池是在內存中預設好一定數量的連接對象,以備用戶在進行數據庫操作時直接使用性能數據庫連接的建立、斷開均由管理池統一管理連接池技術與傳統數據庫連接的比較數據庫操作性能得到提升通過連接池管理數據庫的連接與釋放、提高了系統資源的使用效率為什么使用連接池?Ø傳統數據庫連接方式的不足Ø每一次請求時均需要與數據庫進行連接,資源占用較多Ø當并發訪問數量較大時,網站速度收到極大影響Ø在訪問結束后必須要關閉連接釋放資源Ø系統的安全性和穩定性相對較差企業級開發需要穩健和高效的數據訪問層完成對數據庫的CRUD操作能夠處理數據庫發生的各種錯誤可以靈活的修改配置提供方便使用的工具高性能訪問數據源第一步:Tomcat的conf/context.xml中的配置加入數據庫驅動文件把數據庫驅動的.jar文件,加入到Tomcat的common/lib中應用程序的web.xml文件的配置在web.xml中配置<resource-ref>
<?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" />    -->--紅色字體是需要根據數據庫名配置<Resource name="jdbc/liuyan" auth="Container"type="javax.sql.DataSource" maxActive="100"maxIdle="30" maxWait="10000" username="epet" passWord="123456"driverClassName="oracle.jdbc.driver.OracleDriver"url="jdbc:oracle:thin:@localhost:1521:orcl"/></Context>

import java.sql.Connection;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;/**  * 文件名:JNDI.java * 描    述:這是一個數據庫連接類 * 作    者:WLX * 所屬項目:MyNews * JDK版本:JDK1.7  * 創建時間:2015年5月23日 下午2:54:24 **/public class JNDI {    public Connection getconn() {                Connection conn = null;        try {                        Context ctx;            ctx = new InitialContext();            //java:comp/env/為前綴            DataSource source = (DataSource)ctx.lookup("java:comp/env/jdbc/liuyan");                    Connection connection = source.getConnection();            if(connection != null){                conn = connection;            }                    } catch (NamingException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }        return conn;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 班玛县| 滦南县| 昭觉县| 通许县| 东宁县| 清水河县| 平和县| 凤山县| 渭源县| 兴城市| 泽库县| 九寨沟县| 舒城县| 雷波县| 泽普县| 平舆县| 临城县| 尚志市| 得荣县| 金川县| 吐鲁番市| 义马市| 商水县| 哈巴河县| 托克逊县| 乐山市| 云南省| 顺义区| 南川市| 嘉善县| 库伦旗| 多伦县| 垦利县| 民勤县| 洪湖市| 中方县| 万宁市| 双流县| 上犹县| 罗山县| 黄骅市|