• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

jboss4。0下mysql数据源的配置

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 18次 | 进入软件测试论坛讨论

领测软件测试网 花了一个小时的时间,搞了一下jboss4。0下mysql数据源的配置。下面是一些具体过程
1、首先安装mysql数据库并将其驱动程序考到jboss的server\default\lib下面
2、将mysql-ds.xml文件放置到server\default\deploy下面
内容如下:
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>root</user-name>
<password>qwe123</password>

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>

</datasources>


3、建立一个自己的war包 testds.war
内含有META-INF/jbosscmp-jdbc.xml, 重载默认的数据源
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySql</datasource-mapping>
</defaults>
</jbosscmp-jdbc>
4、建立一个servlet进行测试,放在上面的包中
package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.io.*;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class myservlet extends HttpServlet {

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(arg0, arg1);
PrintWriter out = res.getWriter();

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:/MySqlDS");
conn = ds.getConnection();
if (conn != null)
out.println("ok");
else
out.println("error");
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>my Servlet</servlet-name>
<servlet-class>test.myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>my Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

呵呵。启动jboss,在浏览器中键入http://localhost:8080/testds/ 显示OK,成功!!!



文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网