花了一个小时的时间,搞了一下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,成功!!!
[MySql]PHP存取 Mysql 数据乱码终极解决方案 [MySql]解决Table xxx is marked as crashed and should … [MySql][MySQL]快速解决"is marked as crashed and shoul… [MySql]MySQL DELETE语法用法详解 [MySql]mysql中时间日期格式化 [MySql]修改mysql导入文件大小限制 [其他]MySql常用命令大全 [Web开发]把ACCESS的数据导入到Mysql中的方法详解 [MySql]解决mysql 1040错误Too many connections的方法 [系统软件]利用crontab系统每天定时备份MySQL数据库
|