jboss4。0下mysql数据源的配置
花了一個小時的時間,搞了一下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.cnblogs.com/tran/archive/2004/12/25/81842.html
總結
以上是生活随笔為你收集整理的jboss4。0下mysql数据源的配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET学习杂记
- 下一篇: Mysql远程登录及常用命令