0
点赞
收藏
分享

微信扫一扫

tomcat6中配置JNDI



评:

tomcat6配置JNDI
在Java Web开发中都要与数据库打交道,为了不频繁地打开和关闭数据库,以减少数据库操作负荷,可使数据库在开发过程中保持打开状态,在这里我们采用配置数据源的方式(JNDI),而不是传统地JDBC方式。下面就针对常规型的MySQL5.0.15和Tomcat6.0的数据源的基本配置进行简单的介绍:
  首先声明,如果数据源没有配置好的话,在开发过程中会抛出诸如下列异常等:
  1、org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
  2、Caused by: java.sql.SQLException: No suitable driver
  3、Name jdbc is not bound in this context
现在开始讲下如何配置好数据源同时也是解决上述异常的办法:
方案一:
  步骤一、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context> 中加入如下代码(要根据自己的情况稍加修改):

<Resource name="jdbc/TestDB"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  maxIdle="30"  maxWait="5000"/>


  步骤二、在工程应用中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):

<resource-ref> 
    <description>MySQL DataSource</description> 
     <res-ref-name>jdbc/myznt</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/TestDB"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  maxIdle="30"  maxWait="5000"/>


步骤二、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context>中加入并修改成如下代码(要根据自己的情况稍加修改):

<Context path="/znt" debug="1" reloadable="true"  docBase="E:\EclipseWorkPlace\MyZNT\WebRoot"> 
<ResourceLink global="jdbc/myznt" name="jdbc/myznt" type="javax.sql.Datasource"/>


................<!--此间可能有系统其它自配的内容,可不管-->
</Context>
步骤三、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):

<resource-ref> 
    <description>MySQL DataSource</description> 
     <res-ref-name>jdbc/myznt</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="/znt" docBase="E:\EclipseWorkPlace\MyZNT\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/myznt"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  maxIdle="30"  maxWait="5000"/> 
</Context>


步骤二、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):

<resource-ref> 
    <description>MySQL DataSource</description> 
     <res-ref-name>jdbc/myznt</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目录下。
    通过以上三步,大部分时候还是起作用的,但有时会出现异常,因此不建议使用。
    以上几种方案在实践中经受了测试,方案一和二比较稳定,方案三最好别用,同时只是进行了大致地归纳,其中的哪些地方没有必要或哪里欠妥还没有去测试,望读者进行批评指正。
--------------------------------------------------------
Tomcat链接数据源的基本代码

Context initContext; 
try { 
    initContext = new InitialContext(); 
    Context envContext  = (Context)initContext.lookup("java:/comp/env"); 
    DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB"); 
    Connection conn = ds.getConnection(); 
    Statement stmt = conn.createStatement(); 
    ResultSet set = stmt.executeQuery("SELECT id,name,age FROM user_lzy"); 
    while(set.next()){ 
System.out.println(set.getString("name")); 
    } 
    //etc. 
} catch (NamingException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (SQLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
}

举报

相关推荐

0 条评论