沒想到看上去挺簡(jiǎn)單的一件事情反而花了我半天時(shí)間,郁悶得要命。還是有必要把正確的整理清楚。
我是在Eclipse3.1下調(diào)用Tomcat5.0,數(shù)據(jù)庫是MS SqlServer2000 sp4,驅(qū)動(dòng)程序是jtds1.1。
第一步,假設(shè)我現(xiàn)在的應(yīng)用程序目錄是Sample,把數(shù)據(jù)驅(qū)動(dòng)程序jtds**.jar放到Tomcat安裝目錄的common\lib和應(yīng)用程序目錄webapps\Sample\WEB-INF\lib下。運(yùn)行Tomcat,打開瀏覽器,進(jìn)入Tomcat的管理界面,http://localhost:8080/admin,登陸后,選擇Resource , DataSources,下拉菜單,Create New DataSource。
JNDI名稱:jdbc/mssql
DataSource URL:jdbc:jtds:sqlserver://localhost/Northwind。(相應(yīng)的修改IP和數(shù)據(jù)庫名稱)
JDBC Driver Class:net.sourceforge.jtds.jdbc.Driver
User Name:sa
Password:*******(你的數(shù)據(jù)庫密碼)
其他按照默認(rèn)。save,commit changes, log out,重啟Tomcat。
第二步,打開應(yīng)用程序目錄 webapps\Sample里面的WEB-INF\web.xml,在最后</web-app>前添加:
<resource-ref>
<description>MS SQL Server</description>
<res-ref-name>jdbc/mssql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
如果沒有添加這一步,就會(huì)報(bào)這樣的錯(cuò)誤:Name jdbc is not bound in this Context 。
第三步,打開Tomcat安裝目錄下的conf\Catalina\localhost,找到和你的應(yīng)用程序同名的xml,例如Sample.xml。如果沒有,就建一個(gè)。Sample.xml里面的內(nèi)容是:
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="This is a Sample" docBase="Sample" path="/Sample" workDir="work\Catalina\localhost\Sample">
<ResourceLink name="jdbc/mssql" global="jdbc/mssql" type="javax.sql.DataSource"/>
</Context>
要注意的是,文件名、docBase、path、workDir都不能錯(cuò),要和你應(yīng)用程序名稱相對(duì)應(yīng)。
如果第二步正確了,但是還沒有添加這一步,就會(huì)報(bào)這樣的錯(cuò)誤:Cannot create JDBC driver of class "for connect URL 'null'" 。
posted on 2009-09-04 11:59
super_nini 閱讀(311)
評(píng)論(0) 編輯 收藏