配置數據源 java
數據源的配置有以下三種方法: mysql
方法 一:
在Tomcat6.0 解壓目錄 conf 下找到 context.xml, 在其中的 <Context></Context> 中加入以下代碼: web
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" apache
password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver" 測試
url="jdbc:mysql://localhost/exp01" maxActive="100" maxIdle="30" maxWait="5000"/> url
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/exp01" maxActive="100" maxIdle="30" maxWait="5000"/>
工程應用中/WEB-INF/ 下的 web.xml 中加入以下代碼:spa
<resource-ref> code
<description>MySQL DataSource</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref> <description>MySQL DataSource</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
把MySQL-Connector-java-3.0.12-bin.jar( 可換更高版本 ) 加到 Tomcat 安裝目錄中的 lib 目錄下和工程中的 lib 目錄下。
方 法 二:
在Tomcat6.0 解壓目錄 conf 下找到 server.xml, 在其中的 <GlobalNamingResources></GlobalNamingResources> 中加入以下代碼:
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost/exp01" maxActive="100" maxIdle="30" maxWait="5000"/>
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/exp01" maxActive="100" maxIdle="30" maxWait="5000"/>
在Tomcat6.0 解壓目錄 conf 下找到 context.xml, 在其中的 <Context></Context> 中加入並修改爲以下代碼:
<Context path="/RegisterSys" debug="1" reloadable="true" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot">
<ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.Datasource"/>
<!--此處可能有系統其它自動生成的內容 -->
</Context>
<Context path="/RegisterSys" debug="1" reloadable="true" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot"> <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.Datasource"/> <!--此處可能有系統其它自動生成的內容 --> </Context>
工程中/WEB-INF/ 下的 web.xml 中加入以下代碼:
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref> <description>MySQL DataSource</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
MySQL-Connector-java-3.0.12-bin.jar( 或更高版本 ) 加到 Tomcat 安裝目錄中的 lib 目錄下和工程中的 lib 目錄下。
方法三:
在Tomcat6.0 解壓目錄 conf 下找到 server.xml, 在其中的 <Host></Host> 中加入以下代碼:
<Context path="/RegisterSys" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_MysqlTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" password="lectery"
username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/exp01"
maxActive="100" maxIdle="30" maxWait="5000"/>
</Context>
<Context path="/RegisterSys" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot" debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_MysqlTest_log." suffix=".txt" timestamp="true"/> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/exp01" maxActive="100" maxIdle="30" maxWait="5000"/> </Context>
工程中/WEB-INF/ 下的 web.xml 中加入以下代碼:
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref> <description>MySQL DataSource</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
把MySQL-Connector-java-3.0.12-bin.jar( 或更高版本 ) 加到 Tomcat 安裝目錄中的 lib 目錄下和工程中的 lib 目錄下。 經過以上三步,大部分時候仍是起做用的,但有時會出現異常,所以不建議使用。 以上幾種方案在實踐中經受了測試,方法一和二比較穩定,方發三比較不穩定,因此對於方法三的採用須謹慎。爲簡單起見,本實驗中採用了方法一的作法,通過測試數據源配置成功。