日期:2014-05-20  浏览次数:20798 次

tomcat下怎么配置jndi呢,谁有做好的例子呢
tomcat下怎么配置jndi呢,谁有做好的例子呢

------解决方案--------------------
jndi?这个很简单的,网上多的是例子

给你看个例子

1,将数据库驱动程序拷贝到 $CATALINA_HOME/lib 目录下面。
2,在Context节点下面加上资源节点,如下:
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="" driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://127.0.0.1:3306/test?

characterEncoding=GBK&amp;useUnicode=TRUE"/>
注意的是:如果有&字符,需要转移成&amp;(XML文件规范)
3,加上资源引用:Resource-Def,如下:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

使用数据源和数据库连接:

 

Context initCtx = new InitialContext(); javax.sql.DataSource ds=(javax.sql.DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB"); Connection conn = ds.getConnection();

------解决方案--------------------
部署时有两种方法:
1,在 $CATALINA_HOME\config\catalina\localhost下新建test.xml。
2,在你的web-app应用的MEAT-INF下新建context.xml。 
 
<?xml version="1.0" encoding="UTF-8"?>
<Context debug="0" reloadable="false" crossContext="true" verbosity="debug">
 <Resource name="jdbc/evaluate_dbcp_connect"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test01?useUnicode=true&amp;characterEncoding=UTF-8"
username="root"
password="123456"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true" 
maxActive="100"
maxIdle="30"
maxWait="10000" >
 </Resource>
</Context>


------解决方案--------------------
http://eaststone.iteye.com/blog/1011365
参考下