做 tomcat jndi 一共分三部分:
我用的是tomcat 6.0
1:把sql驱动包放到tomcat中lib包下.
2:在/conf/context.xml添加
<Resource name="TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
testOnBorrow="true" testOnReturn="true" testWhileIdle="true"
validationQuery=" select 1 "
username="root" password="000000" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/test"/>
3 :在你的项目中的web.xml添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
对于第步 在网上很多人都有这么写我也是这么写的,成功了,我为了更深一步的了解tomcat jndi的配置进行了分部测式。(按照上面写一定没有问题)我故意破坏一处其它不变,得到如下结果
1 把1删除 error信息找不到驱动。
2 把2删除 error找不到连接的url
3 把3删除 没有错能正常运行
测式代码
package jing.lian.kui.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import javax.naming.*;
public class DBPoolUtils {
private Connection conn = null;
Context context = null;
DataSource dataSource = null;
public Connection getConnection() throws NamingException, SQLException{
context = new InitialContext();
dataSource = (DataSource) context.lookup("java:/comp/env/TestDB");
if(dataSource!=null){
conn = dataSource.getConnection();
}
return conn;
}
public void getAll(){
DBPoolUtils db = new DBPoolUtils();
Statement st = null;
ResultSet rs = null;
try {
Connection conn = db.getConnection();
st = conn.createStatement();
rs = st.executeQuery("select * from stuinfo");
while(rs.next()){
System.out.println(rs.getString("stuName"));
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(st!=null){
st.close();
st = null;
}
if(rs!=null){
rs.close();
rs =null;
}
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
分享到:
相关推荐
TOMCAT JNDI配置
Tomcat JNDI配置说明
Tomcat JNDI 数据源 自动生成程序 Servlet 版 本程序自动判断Tomcat版本,兼容tomcat 5.0、5.5、6.0等版本, 根据项目名自动生成 JNDI 配置文件,使程序员从手动配置JNDI数据源中解脱出来。 使用说明: 1、把...
Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程 Tomcat jndi 配置数据源过程
tomcat jndi 数据库配置方法
简单我tomcat5.0中的JNDI应用
tomcat jndi 的困惑 Cannot create JDBC driver of class '' for connect URL 'null'
TOMCAT8 JNDI对用户名和密码加密
自定义jndi数据源factory类,用于解密jndi中的加密数据,解密方式为DES,具体可根据实际需求修改。
tomcat-jndi
自己总结的三种使用方式,对比来看选择最适合你的方式
tomcat上配置的JNDI数据源的几种方式
tomcat配置jndi、数据库连接jndi、jndi实例、非常简单,可以快速学习入门。有连接数据库的例子,和具体的配置。
hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...
NULL 博文链接:https://buliedian.iteye.com/blog/858026
NULL 博文链接:https://xurichusheng.iteye.com/blog/1005452
tomcat7.0的集中jndi创建方法和tomcat配置ssl
Tomcat6+spring+jndi配置数据源说明.docx
Spring Boot 2.x嵌入式tomcat JNDI数据源(开发配置文件)+ WAR 该代码旨在通过JNDI数据源查找保持数据库访问。 在开发中,使用配置了JNDI数据源的嵌入式tomcat运行在部署为WAR时,它将在服务器上查找真实的JDNI...
tomcat配置jndi几个方式