1.什麼是JNDI?
中文:java名稱目錄接口 一種技術
2.JNDI做什麼?
JNDI可以把java應用程式通路資料庫管理和配置交給存放java應用的伺服器(tomcat)。
3.沒有JNDI之前?
JDBC連接配接資料庫,java應用程式與mysql提供的驅動程式關聯 映射一個Driver的Class對象(Driver的path:com.mysql.jdbc.Driver)
然後用java提供的DriverMannager管理類調用getConnection(“jdbc:mysql://localhost:8080/資料庫名字”,“使用者名”,“密碼”)方法
獲得一個connection連接配接對象,說明java與指定資料庫建立連接配接。
缺點:
*當所有程式調試完成後,所有java源檔案形成jar包,如果資料庫類型改變orcal或使用者名改變,必須改源代碼
4.有JNDI之後
把變化的地方交給伺服器來設定建立一個資料源,需要JDBC建立連接配接時引用,交給伺服器管理配置,如果java應用程式需要就去那一個
5.使用JNDI建立資料庫連接配接池
第一步:安裝java應用伺服器根目錄找到conf檔案夾裡放的伺服器上下文配置檔案context編輯後增加資料源代碼:
<Resource name=“jdbc/news” auth=“Container” type=“javax.sql.DataSource”//資料源名字 驗證資料源的容器 類型
maxActive=“100” maxIdle=“30” maxWait=“10000” username=“root”//最大連接配接資料庫連接配接對象的數量100 最大空閑數是30(例如50個空閑等待N時間後變成maxWait狀态)資料庫使用者名
password=“123” driverClassName=“com.mysql.jdbc.Driver” //資料庫密碼 驅動程式名稱(mysql提供)
url=“jdbc:mysql://localhost:3306/newsdb?useUnicode=true&characterEncoding=utf8”//建立連接配接的路徑,編碼格式UTF-8
/>
第二步:把java即将操作的資料庫的驅動檔案導入java應用伺服器,粘貼至java應用伺服器根目錄lib檔案夾(作用放程式需用引用的jar包),放入此包的jar
java應用伺服器上所有項目都可以用。
第三步:web項目上web.xml進行注冊:
<resource-ref>
<res-ref-name> jdbc/news </res-ref-name>//名字與資料源名字一緻
<res-type> javax.sql.DataSource </res-type>//類型:DateSource
<res-auth> Container </res-auth>
</resource-ref>
第四步:java應用程式處引用資料源
Context ic=new InitialContext();//接口、實作類
DataSource source=(DataSource)ic//實作類對象.lookup(“java:comp/env/jdbc/news”);//調用lookup方法傳回一個資料源類型DateSource
conn=source.getConnection();//生成JDBC連接配接對象
<li class="tool-item tool-active is-like "><a href="javascript:;" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup" target="_blank" rel="external nofollow" ></use>
</svg><span class="name">點贊</span>
<span class="count">6</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G" target="_blank" rel="external nofollow" ></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang" target="_blank" rel="external nofollow" ></use>
</svg>分享</a></li>
<!--打賞開始-->
<!--打賞結束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章舉報</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/qidao12" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<img src="https://profile.csdnimg.cn/5/2/0/3_qidao12" class="avatar_pic" username="qidao12">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/10.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/qidao12" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" data-report-click="{"mod":"popu_379"}" target="_blank">qidao12</a></span>
</div>
<div class="text"><span>釋出了20 篇原創文章</span> · <span>獲贊 15</span> · <span>通路量 3萬+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=qidao12" target="_blank" rel="external nofollow" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">關注</a>
</div>
</div>
</div>