一
把數據庫JDBC驅動拷貝到
%TOMCAT_HOME%/common/lib
或
%TOMCAT_HOME%/webapps/yourweb/WEBINF/lib 下;
修改tomcat安裝目錄下conf/serverxml在Host元素下添加一個Context元素示例:
< Context path=/TomcatPool docBase=E:\\Tomcat \\webapps\\TomcatPool debug= reloadable=true>
< Resource name=jdbc/ds type=javaxsqlDataSource
driverClassName=commicrosoftjdbcsqlserverSQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:;databasename=pubs
username=sa password=sa
maxWait= maxActive=
maxIdle=/>
< /Context>
修改TomcatPool工程的web
xml
添加一個resource
ref元素
< resourceref>
< description>sqlserverDB connection< /description>
< resrefname>jdbc/ds< /resrefname>
< restype>javaxsqlDataSource< /restype>
< resauth>Container< /resauth>
< /resourceref>
調用
javaxnamingContext context = new javaxnamingInitialContext();
javaxsqlDataSource ds = (javaxsqlDataSource)contextlookup(java:comp/env/jdbc/ds);
javasqlConnection conn = dsgetConnection();
對於Tomcat 步驟為
以前的版本要到serverxml中配置版本以後要到contextxml中配置
在下只能用這個配置:
< Context docBase=MyZhiNangTuanDemo path=/MyZhiNangTuanDemo reloadable=true>
< Resource name=jdbc/mldn auth=Container
type=javaxsqlDataSource maxActive= maxIdle=
maxWait= username=root password=
driverClassName=commysqljdbcDriver
url=jdbc:mysql://localhost:/mldn/>
< ResourceLink global=jdbc/mldn name=jdbc/mldn type=javaxsqlDataSource/>
< /Context>
應用程序的wen
xml也不用配置這個了
< resourceref>
< description>DB Connection< /description>
< resrefname>jdbc/mldn< /resrefname>
< restype>javaxsqlDataSource< /restype>
< resauth>Container< /resauth>
< /resourceref>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28435.html