配置数据源 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 目录下。 经过以上三步,大部分时候仍是起做用的,但有时会出现异常,所以不建议使用。 以上几种方案在实践中经受了测试,方法一和二比较稳定,方发三比较不稳定,因此对于方法三的采用须谨慎。为简单起见,本实验中采用了方法一的作法,通过测试数据源配置成功。