Wednesday, December 1, 2010

Connecting to MySQL in JBOSS through Eclipse.

To connect to MySQL in a JBOSS Application.
1)Copy mysql-connector-java-3.0.9-stable-bin.jar to the /server/default/lib directory.


2)copy /docs/examples/jca/mysql-ds.xml to the /server/default/deploy


Contents of the mysql-ds.xml file




<!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->


<!-- $Id: mysql-ds.xml 88948 2009-05-15 14:09:08Z jesper.pedersen $ -->


<!-- Datasource config for MySQL using 3.0.9 available from: http://www.mysql.com/downloads/api-jdbc-stable.html -->


<datasources>


<local-tx-datasource>


<jndi-name>HypatiaDS</jndi-name>


<connection-url>jdbc:mysql://localhost:3306/test</connection-url>


<driver-class>com.mysql.jdbc.Driver</driver-class>


<user-name>root</user-name>


<password></password>


<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>


<!-- should only be used on drivers after 3.22.1 with "ping" support <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name><br />-->


<!-- sql to call when connection is created <new-connection-sql>some arbitrary sql</new-connection-sql><br />-->


<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql><br />-->


<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->


<metadata>


<type-mapping>mySQL</type-mapping>


</metadata>


</local-tx-datasource>


</datasources>






A JSP File to check the Connection

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>










<meta equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<title>Connecting To MySql</title>






Connecting To MySql


<%javax.naming.InitialContext initialContext = new javax.naming.InitialContext(); javax.sql.DataSource ds = (javax.sql.DataSource) initialContext.lookup("java:HypatiaDS"); java.sql.Connection conn = ds.getConnection(); out.println(conn); %>

No comments:

Post a Comment