DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

配置tomcat5.5 jndi 各种配置方式 分析总结(mysql)

准备工作:
安装tomcat5.5(注意这点)
安装mysql
拷贝mysql驱动到tomcat_home/common/lib下
新建一个web工程
在工程中加入index.jsp
<%@page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*" %>
<%@page contentType="text/html;charset=BIG5"%>
<%    
    Context ctx 
= new InitialContext();      
    String strLookup 
= "java:comp/env/jdbc/test"
    DataSource ds 
=(DataSource) ctx.lookup(strLookup);
    Connection con 
= ds.getConnection();
    
if (con != null){
        out.print(
"success");
    }
else{
        out.print(
"failure");
    }
       
%>
web.xml中加入
<resource-ref>
    
<res-ref-name>jdbc/test</res-ref-name>
    
<res-type>javax.sql.DataSource</res-type>
    
<res-auth>Container</res-auth>
    
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

配置tomcat
这一步的目的就是告诉tomcat如何连接数据库
可以分为两种大的类型,每种类型又有很多种配置方式
配置类型一;
(直接配置的类型,这种方式最简单)

方法一:
直接在tomcat_home/conf/localhost/下建立一个xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,对应的名字叫jdni.xml
内容如下:

<Context>
 
<Resource
          name
="jdbc/test"
          type
="javax.sql.DataSource"
          password
="bb"
          driverClassName
="com.mysql.jdbc.Driver"
          maxIdle
="2"
          maxWait
="50"
          username
="root"
          url
="jdbc:mysql://localhost:3306/test"
          maxActive
="4"/>
</Context>

方法二:
只需在tomcat_home\webapps\myapps\META-INF\context.xml中增加:
<context>
<Resource
          name
="jdbc/test"
          type
="javax.sql.DataSource"
          password
="bb"
          driverClassName
="com.mysql.jdbc.Driver"
          maxIdle
="2"
          maxWait
="50"
          username
="root"
          url
="jdbc:mysql://localhost:3306/test"
          maxActive
="4"/>
</context>


说明:这种配置需要告诉tomcat resource的内容,resource应用于什么地方
第一种方法通过文件名知道了app的name
第二种方式本身就在app内部,所以name肯定知道
两种方式都要放在context中

配置类型二:
(配置全局resource,然后通过resourcelink来映射)

步骤一:配置全局resource(这一步对于所有的配置都是一样的)
打开tomcat_home/conf/server.xml加入

<Resource
          name
="jdbc/test"
          type
="javax.sql.DataSource"
          password
="bb"
          driverClassName
="com.mysql.jdbc.Driver"
          maxIdle
="2"
          maxWait
="50"
          username
="root"
          url
="jdbc:mysql://localhost:3306/test"
          maxActive
="4"/>


步骤二:映射
(映射可以配置在多个地方,也就有多个配置方法:)

方法一:(对比类型一的配置理解)
直接在tomcat_home/conf/localhost/下建立一个xml文件,文件名是<yourAppName>.xml
例如我的工程名叫jndi,对应的名字叫jdni.xml加入如下内容

<Context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>
</Context>

方法二:(对比类型一的配置理解)
在tomcat_home\webapps\myapps\META-INF\context.xml的Context中增加:
<context>
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/> 
</context>


方法三:(上边两种方法都是把全局的resource 映射给jndi这个web应用,第三种方法就是把这个
全局的resource直接公开给所有的应用)
在tomcat_home/conf/context.xml的<Context></context>之间加入
<ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource"/>

运行测试:
打开ie,输入http://localhost:8080/jndi/index.jsp
看到success

常见错误:
1,Name jdbc is not bound in this Context
2,Cannot create JDBC driver of class '' for connect URL 'null' conf localhost
原因:
大多数是因为配置了全局的resource,但没有link造成的。
解决:
加入link就行了,link的方式见类型二的三种方法。

分析:
看到上边这么多方法,是否感觉眼花缭乱,其实不要死记配置,按照原理分析一下就好了。
你需要的是告诉tomcat哪个应用如何连接数据库。

类型一的方式对应一个应用单独使用这个配置的情况
就是直接告诉tomcat"应用名"  "连接数据库需要的参数"

类型二的方式对应多个应用共享一个配置的情况
这样先配置server.xml告诉tomcat全局范围的"连接数据库需要的参数"
然后映射,映射的时候
1,如果不知道"app name(应用名)"就只需要通过文件名来传递这个信息
2,如果"app name"都知道就只需要加入映射的内容
3,如果要配置成全局公用的,就不需要"app name",本身放在tomcat的context.xml中

最后再次提醒一下:所有的配置必须放在<context></context>之间

posted on 2007-05-07 22:27 dreamstone 阅读(6805) 评论(2)  编辑  收藏 所属分类: 利器基础片段

评论

# re: 配置tomcat5.5 jndi 各种配置方式 分析总结(mysql) 2007-05-07 23:03 杨爱友

楼主能用SQLSERVER配置好吗?用JNDI我一直连接不上SQLSERVER数据库  回复  更多评论   

# re: 配置tomcat5.5 jndi 各种配置方式 分析总结(mysql) 2007-05-07 23:33 dreamstone

sqlserver和mysql的区别不大。主要几个地方:
1,sqlserver的jdbc驱动是三个文件,mysql是一个。
2,把文中多次出现的这一部分换成
<Resource
name="jdbc/test"
type="javax.sql.DataSource"
username="sa"
password="your password" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="50"
DatabaseName="your db name"
url="jdbc:microsoft:sqlserver://localhost:1433"
maxActive="4"/>
只需要更改url ,username ,password,driverClassName并加入databasename就可以了。
没经过测试,凭着记忆写的,不过应该是对的。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: