JSP+Severlet+JDBC

配置数据源

数据源的配置有如下三种方法:

方法 一:
Tomcat6.0 解压目录 conf 下找到 context.xml, 在其中的 <Context></Context>  中加入如下代码:

Java代码  

  1. <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"   

  2.     password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver"  

  3.  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"/>

  工程应用中/WEB-INF/ 下的 web.xml 中加入如下代码:

Java代码  

  1. <resource-ref>  

  2.   <description>MySQL DataSource</description>  

  3.   <res-ref-name>jdbc/mysql</res-ref-name>  

  4.   <res-type>javax.sql.DataSource</res-type>  

  5.   <res-auth>Container</res-auth>  

  6. </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> 中加入如下代码:

Java代码  

  1. <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"   

  2. password="lectery" username="root" driverClassName="org.gjt.mm.mysql.Driver"   

  3. 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> 中加入并修改成如下代码:

Java代码  

  1. <Context path="/RegisterSys" debug="1" reloadable="true" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot">  

  2. <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.Datasource"/>  

  3.          <!--此处可能有系统其它自动生成的内容 -->  

  4. </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 中加入如下代码:

Java代码  

  1. <resource-ref>  

  2. <description>MySQL DataSource</description>  

  3. <res-ref-name>jdbc/mysql</res-ref-name>  

  4. <res-type>javax.sql.DataSource</res-type>  

  5. <res-auth>Container</res-auth>  

  6. </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> 中加入如下代码:

Java代码  

  1. <Context path="/RegisterSys" docBase="E:\EclipseWorkPlace\RegisterSys\WebRoot"  

  2. debug="5" reloadable="true" crossContext="true">  

  3.   

  4. <Logger className="org.apache.catalina.logger.FileLogger"  

  5. prefix="localhost_MysqlTest_log." suffix=".txt"  

  6. timestamp="true"/>  

  7. <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" password="lectery"   

  8. username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/exp01"   

  9. maxActive="100" maxIdle="30" maxWait="5000"/>  

  10. </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 中加入如下代码:

Java代码  

  1. <resource-ref>  

  2.   <description>MySQL DataSource</description>  

  3.   <res-ref-name>jdbc/mysql</res-ref-name>  

  4.   <res-type>javax.sql.DataSource</res-type>  

  5.   <res-auth>Container</res-auth>  

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

你可能感兴趣的:(jsp)