準(zhǔn)備工作:
安裝tomcat5.5(注意這點(diǎn))
安裝mysql
拷貝mysql驅(qū)動(dòng)到tomcat_home/common/lib下
新建一個(gè)web工程
在工程中加入index.jsp
<
%@page
import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*" %>
<
%@page
contentType="text/html;charset=BIG5"%>
<%???
??? Context ctx = new InitialContext();?????
??? String strLookup = "java:comp/env/jdbc/test";
??? DataSource ds =(DataSource) ctx.lookup(strLookup);
??? Connection con = ds.getConnection();
??? if (con != null){
??????? out.print("success");
??? }else{
??????? out.print("failure");
??? }??????
%>web.xml中加入
<resource-ref>
??? <res-ref-name>jdbc/test</res-ref-name>
??? <res-type>javax.sql.DataSource</res-type>
??? <res-auth>Container</res-auth>
??? <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
配置tomcat
這一步的目的就是告訴tomcat如何連接數(shù)據(jù)庫(kù)
可以分為兩種大的類型,每種類型又有很多種配置方式
配置類型一;
(直接配置的類型,這種方式最簡(jiǎn)單)
方法一:
直接在tomcat_home/conf/localhost/下建立一個(gè)xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,對(duì)應(yīng)的名字叫jdni.xml
內(nèi)容如下:
<Context>
?<Resource
????????? name="jdbc/test"
????????? type="javax.sql.DataSource"
????????? password="bb"
????????? driverClassName="com.mysql.jdbc.Driver"
????????? maxIdle="2"
????????? maxWait="50"
????????? username="root"
????????? url="jdbc:mysql://localhost:3306/test"
????????? maxActive="4"/>
</Context>
方法二:
只需在tomcat_home\webapps\myapps\META-INF\context.xml中增加:
<context>
<Resource
????????? name="jdbc/test"
????????? type="javax.sql.DataSource"
????????? password="bb"
????????? driverClassName="com.mysql.jdbc.Driver"
????????? maxIdle="2"
????????? maxWait="50"
????????? username="root"
????????? url="jdbc:mysql://localhost:3306/test"
????????? maxActive="4"/>
</context>
說明:這種配置需要告訴tomcat resource的內(nèi)容,resource應(yīng)用于什么地方
第一種方法通過文件名知道了app的name
第二種方式本身就在app內(nèi)部,所以name肯定知道
兩種方式都要放在context中
配置類型二:
(配置全局resource,然后通過resourcelink來映射)
步驟一:配置全局resource(這一步對(duì)于所有的配置都是一樣的)
打開tomcat_home/conf/server.xml加入
<Resource
????????? name="jdbc/test"
????????? type="javax.sql.DataSource"
????????? password="bb"
????????? driverClassName="com.mysql.jdbc.Driver"
????????? maxIdle="2"
????????? maxWait="50"
????????? username="root"
????????? url="jdbc:mysql://localhost:3306/test"
????????? maxActive="4"/>
步驟二:映射
(映射可以配置在多個(gè)地方,也就有多個(gè)配置方法:)
方法一:(對(duì)比類型一的配置理解)
直接在tomcat_home/conf/localhost/下建立一個(gè)xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,對(duì)應(yīng)的名字叫jdni.xml加入如下內(nèi)容
<Context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
</Context>
方法二:(對(duì)比類型一的配置理解)
在tomcat_home\webapps\myapps\META-INF\context.xml的Context中增加:
<context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
</context>
方法三:(上邊兩種方法都是把全局的resource 映射給jndi這個(gè)web應(yīng)用,第三種方法就是把這個(gè)
全局的resource直接公開給所有的應(yīng)用)
在tomcat_home/conf/context.xml的<Context></context>之間加入
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
運(yùn)行測(cè)試:
打開ie,輸入
http://localhost:8080/jndi/index.jsp
看到success
常見錯(cuò)誤:
1,Name jdbc is not bound in this Context
2,Cannot create JDBC driver of class '' for connect URL 'null' conf localhost
原因:
大多數(shù)是因?yàn)榕渲昧巳值膔esource,但沒有l(wèi)ink造成的。
解決:
加入link就行了,link的方式見類型二的三種方法。
分析:
看到上邊這么多方法,是否感覺眼花繚亂,其實(shí)不要死記配置,按照原理分析一下就好了。
你需要的是告訴tomcat哪個(gè)應(yīng)用如何連接數(shù)據(jù)庫(kù)。
類型一的方式對(duì)應(yīng)一個(gè)應(yīng)用單獨(dú)使用這個(gè)配置的情況
就是直接告訴tomcat"應(yīng)用名"? "連接數(shù)據(jù)庫(kù)需要的參數(shù)"
類型二的方式對(duì)應(yīng)多個(gè)應(yīng)用共享一個(gè)配置的情況
這樣先配置server.xml告訴tomcat全局范圍的"連接數(shù)據(jù)庫(kù)需要的參數(shù)"
然后映射,映射的時(shí)候
1,如果不知道"app name(應(yīng)用名)"就只需要通過文件名來傳遞這個(gè)信息
2,如果"app name"都知道就只需要加入映射的內(nèi)容
3,如果要配置成全局公用的,就不需要"app name",本身放在tomcat的context.xml中
最后再次提醒一下:所有的配置必須放在<context></context>之間
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
