Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 11/18/2004; 5:20:26 PM.

 

Subscribe to "Inside Scoop on J2EE" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Monday, March 29, 2004

Using MySQL database with OC4J

I received few requests to provide the steps to configure OC4J to use MySQL database. You can configure any database if you have a JDBC driver to be used with OC4J, however Oracle supports third-party databases like IBM DB2, Microsoft SQLServer, Sybase, etc. by using DataDirect JDBC drivers. The DataDirect drivers can be dowloaded from OTN (http://otn.oracle.com) for exclusive use with Oracle Application Server. In this blog entry, I will provide the steps to configure OC4J to use MySQL database. Please note that this note is for demonstration purpose only and is not officially supported by Oracle.

Assumption

This example assumes MySQL database is installed and configured. Let us assume that you have a name named EMP created in the Test database. Also you have downloaded the MySQL Connector/J JDBC driver from http://www.mysql.com/downloads/api-jdbc.html and readily available for use. Also I'm assuming that you are using OC4J 9.0.4 production or OC4J 10.0.3 Developer preview. You can download OC4J from http://otn.oracle.com/tech/java/oc4j/index.html.

Configure OC4J

1. Install JDBC Diver

You have to copy the JDBC driver library i.e. mysql-connector-java-3.0.11-stable-bin.jar to %OC4J_HOME%/j2ee/home/applib directory.

2. Create DataSource

Create the datasource entry for connecting to the MySQL database in the %OC4J_HOME%/j2ee/home/config/data-sources.xml as follows:

<data-source

class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"

name="mysqlDS"

location="jdbc/mysqlCoreDS"

xa-location="jdbc/xa/mysqlXADS"

ejb-location="jdbc/mysqlDS"

connection-driver="com.mysql.jdbc.Driver"

username="root"

password="welcome"

url="jdbc:mysql://144.25.134.24/Test"

inactivity-timeout="30"

/>

3. Test DataSource

You have to restart OC4J before you can use the DataSource you created in the previous step. Here is a sample JSP that you can use to test the DataSource that you created in the previous step.

<%@ page import="java.sql.*, javax.naming.*, javax.sql.*" %>

<% try { InitialContext ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("jdbc/mysqlDS");

Connection con = ds.getConnection(); Statement statement = con.createStatement();

ResultSet rset = statement.executeQuery("select * from emp"); %>

<h1> Test OC4J with MySQL DB </h1>

<table border=1 align=center>

 <% while (rset.next()) { %>

 <TR><TD>

<%= rset.getInt(1) %>

 <TD>

<%= rset.getString(2) %>

<% } statement.close();

con.close(); }

catch(Exception e)

{ out.print(e.getMessage()); }

%>

 

Conclusion

You can use any third-party database like MySQL with OC4J.


12:46:16 PM    comment []

© Copyright 2004 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


March 2004
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Feb   Apr