如何在TOMCAT中配置jboss jndi数据源配置通过JNDI访问测试操作步骤

JNDI:如何在TOMCAT中配置数据源通过JNDI访问测试操作步骤 - simpledev - ITeye技术网站
博客分类:
JNDI:Java Naming and Directory Interface
中文翻译:Java命名和目录接口
实现功能:在Tomcat中配置Mysql数据源,然后通过JNDI测试工程测试是否配置成功
开发环境:MyEclipse5.0GA& Tomcat/5.5.12
接下来的就是操作步骤:
1〉在Mysql中Test数据库中创建表student
create table student(
& id int not null primary key,
& name varchar(20)
);
--添加三条测试数据:
insert into student values
(1,'张三'),
(2,'李四'),
(3,'王五');
--查看是否添加成功:
select *
数据层完成之后,我们开始创建JNDI测试工程。
2〉在MyEclipse中创建工程jndiTest080220,编辑WEB-INF/web.xml文件,在文件中添加:
&description&MYSQL JNDI TEST&/description&
&resource-ref&
&description&DB Connection test&/description&
&res-ref-name&jdbc/test&/res-ref-name&
&res-type&javax.sql.DataSource&/res-type&
&res-auth&Container&/res-auth&
&/resource-ref&
在WebRoot下新建一个MyJsp.jsp页面,添加以下代码:
&h1&get data from mysql's db named test &/h1&
&&& &hr&
&&& &%
DataSource ds =
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("Initial&& Failed!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
if (ctx != null)
ds = (DataSource) ctx.lookup("jdbc/test");
if (ds == null)
throw new Exception("Look&& up&& DataSource&& Failed!");
} catch (Exception e) {
System.out.println(e.getMessage());
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from student");
while (rs.next()){
&%=rs.getInt(1) %&
&%=rs.getString(2) %&
rs.close();
stmt.close();
conn.close();
%&
web.xml和MyJsp.jsp代码编写完毕之后,我们最后来配置TOMCAT数据源且同时发布我们的工程。
3〉在$tomcat$\Tomcat 5.5\conf\server.xml,对server.xml文件进行编辑,
在&Host&&/Host&之间添加:
&Context path="/jdniTest080220" debug="0" reloadable="true" privileged="true" docBase="E:\HNHJ\java\jndiTest080220" workDir="E:\HNHJ\java\jndiTest080220\WebRoot"&
&&&&&&&&&&
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/test"
username="root"
password=""
maxActive="20"
maxIdle="10"
maxWait="10000" /&
&&&&&&&&&& &/Context&
注意:$tomcat$表示你当前安装tomcat的根目录
4〉启动tomcat,在IE中输入URL:http://localhost:8080/jdniTest080220/WebRoot/MyJsp.jsp
备注:
1.如果页面报“javax.servlet.ServletException: Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver'
”错误,解决方法是在“$tomcat$\Tomcat 5.5\common\lib”下添加mysql-connector-java-3.0.17-ga-bin.jar
2.如果页面报“javax.servlet.ServletException: Cannot create JDBC driver of class '' for connect URL 'null'
”错误,说明你发布工程的方式是通过MyEclipse直接发布上去,然后启动服务进行访问的,解决方法是按照3〉的方式配置&Context&指定工程文件夹物理路径方式进行发布。
3.web.xml文件中res-ref-name名称:jdbc/test和3〉中&Resource name="jdbc/test" ...&必须一致。
浏览: 147184 次
来自: 上海
请问下,你的xfire.xml这个文件是怎么写的,放在哪里了
qq 写道终于找到一个好用的了。
其他的都是 ...
终于找到一个好用的了。
其他的都是原版抄去抄去,真服了.... ...1662人阅读
一、问题描述
由于公司项目中的数据源是配置在Tomcat中的server.xml中的,所以在使用Junit进行单元测试的时候,无法获取数据源。
二、解决方案
由于项目集成了Spring的自动注入等功能,所以在使用Junit进行单元测试的时候需要保证Spring的配置文件都能被加载,同时需要保证连接数据库的数据源必须被加载,这就需要配置单独的数据源,具体方法如下:
新建spring_jndi_test.xml
&?xml version="1.0" encoding="UTF-8"?&
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"&
name="driverClassName" value="oracle.jdbc.OracleDriver" /&
name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:sjk" /&
name="username" value="username" /&
name="password" value="password" /&
在Junit测试类中加载配置文件与获取Bean
public class CommonDAOJdbc_StandardTest {
private volatile static BeanF
public void testGetFirmCanOutBalance() {
CommonDAO commonDAO = (CommonDAO) factory.getBean("commonDAO");
public void init() {
System.out.println("加载spring配置开始 ............");
ArrayList&String& list = new ArrayList&String&();
list.add("spring.xml");
list.add("Spring_jndi_test.xml");
factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()]));
((AbstractApplicationContext) factory).registerShutdownHook();
} catch (Exception e) {
e.printStackTrace();
System.out.println("加载配置文件时发生错误" + e);
System.out.println("加载spring配置结束.............");
至此,便可以进行Junit的单元测试,且数据源也能获取了。
当然,如果出现“java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver”,那么则需要Build Path -& Add Libraries … 引入ojdbc包即可。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:29260次
排名:千里之外
原创:28篇
评论:25条
(3)(1)(2)(2)(1)(2)(2)(2)(1)(9)(8)(1)(4)JAVA技术(25)
JNDI:Java Naming and Directory Interface 中文翻译:Java命名和目录接口 实现功能:在Tomcat中配置Mysql数据源,然后通过JNDI测试工程测试是否配置成功 开发环境:MyEclipse5.0GA& Tomcat/5.5.12 接下来的就是操作步骤: 1〉在Mysql中Test数据库中创建表student
create table student( & id int not null primary key, & name varchar(20) ); --添加三条测试数据: insert into student values (1,'张三'), (2,'李四'), (3,'王五'); --查看是否添加成功: select * 数据层完成之后,我们开始创建JNDI测试工程。 2〉在MyEclipse中创建工程jndiTest080220,编辑WEB-INF/web.xml文件,在文件中添加: &description&MYSQL JNDI TEST&/description& &resource-ref& &description&DB Connection test&/description& &res-ref-name&jdbc/test&/res-ref-name& &res-type&javax.sql.DataSource&/res-type& &res-auth&Container&/res-auth& &/resource-ref& 在WebRoot下新建一个MyJsp.jsp页面,添加以下代码: &h1&get data from mysql's db named test &/h1& &hr&&&% DataSource ds = try { && Context initCtx = new InitialContext(); && if (initCtx == null) &&&&& throw new Exception("Initial&& Failed!"); & Context ctx = (Context) initCtx.lookup("java:comp/env"); & if (ctx != null) &&&&& ds = (DataSource) ctx.lookup("jdbc/test"); & if (ds == null) &&&&&& throw new Exception("Look&& up&& DataSource&& Failed!"); } catch (Exception e) { && System.out.println(e.getMessage()); } %& &% Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from student"); while (rs.next()){ %& & &%=rs.getInt(1) %& & &%=rs.getString(2) %& &% } rs.close(); stmt.close(); conn.close(); %& web.xml和MyJsp.jsp代码编写完毕之后,我们最后来配置TOMCAT数据源且同时发布我们的工程。 3〉在$tomcat$/Tomcat 5.5/conf/server.xml,对server.xml文件进行编辑, 在&Host&&/Host&之间添加: &Context path="/jdniTest080220" debug="0" reloadable="true" privileged="true" docBase="E:/HNHJ/java/jndiTest080220" workDir="E:/HNHJ/java/jndiTest080220/WebRoot"& &&&&&&&&&& &Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost:3306/test" username="root" password="" maxActive="20" maxIdle="10" maxWait="10000" /& &/Context& 注意:$tomcat$表示你当前安装tomcat的根目录 4〉启动tomcat,在IE中输入URL:http://localhost:8080/jdniTest080220/WebRoot/MyJsp.jsp 备注: 1.如果页面报&javax.servlet.ServletException: Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver' &错误,解决方法是在&$tomcat$/Tomcat 5.5/common/lib&下添加mysql-connector-java-3.0.17-ga-bin.jar 2.如果页面报&javax.servlet.ServletException: Cannot create JDBC driver of class '' for connect URL 'null' &错误,说明你发布工程的方式是通过MyEclipse直接发布上去,然后启动服务进行访问的,解决方法是按照3〉的方式配置&Context&指定工程文件夹物理路径方式进行发布。 3.web.xml文件中res-ref-name名称:jdbc/test和3〉中&Resource name="jdbc/test" ...&必须一致.
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:336430次
积分:4874
积分:4874
排名:第4959名
原创:162篇
转载:33篇
评论:33条
(1)(3)(2)(1)(2)(8)(6)(22)(18)(2)(18)(27)(29)(27)(27)(1)(1)JNDI数据源配置_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
JNDI数据源配置
上传于|0|0|文档简介
&&tomcat JNDI数据源配置
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩2页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 tomcat jndi 数据源 的文章

 

随机推荐