? Copyright 2004 Debu Panda.
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.
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:
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 DataSourceYou 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"); %>
Test OC4J with MySQL DB
<table align="center" border="1">
<% while (rset.next()) { %>
<tr><td>
<%= rset.getInt(1) %>
<tbody><tr><td>
<%= rset.getString(2) %>
<td>
<% } statement.close();
con.close(); }
catch(Exception e)
{ out.print(e.getMessage()); }
%>
ไม่มีความคิดเห็น :
แสดงความคิดเห็น