Jason Bennett's Developer Corner

 






Click to see the XML version of this web page.

>


View David Jason Bennett's profile on LinkedIn

 

 

A Little About Jason Bennett ...

I've had an interest in publishing technical articles and HELPFUL code for a few years.  I am (by trade and hobby) a developer who specializes in Oracle technologies and web based architectures.  I have been an employee of both TUSC and Oracle Corporation.  My intent here is to share my ideas and coding experiences with the developer community as a whole.  As with all developers some of my ideas are great and some of them are ....  well you know.  Anyway, I hope you find something here that will aid in your endeavor, or spark a new idea. 

I am more than happy to assist with technical issues and will even write a little code if need be. If you find something on the site that is really useful and you'd like to make a contribution (absolutely up to you and absolutely not required), just click the "Make a Donation" button on the left!

Good luck and good coding !




  Tuesday, May 04, 2004


Creating a SQL Server 2000 Data Source with OracleAS 10g OC4J using Microsoft JDBC Drivers

By Jason Bennett

 

The following steps demonstrate how to create server side data source for connecting to SQL Server from an OracleAS 10g OC4J instance.

 

Step 1. Download SQL Server JDBC drivers from Microsoft (http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&;DisplayLang=en#filelist).

 

Step 2. After installing the drivers, copy .JAR files from <Microsoft SQL Server 2000 Driver for JDBC install directory>lib directory to the <OracleAS 10g HOME>j2ee<OC4J INSTANCE HOME>applib directory.

 

Step 3. Log into Oracle Enterprise Manager 10g and create a data source under the OC4J instance of your choice with following values:

 

Name: SQLServerDS

Description: Datasource used for MS SQL Server connection.

Data Source Class: com.microsoft.jdbcx.sqlserver.SQLServerDataSource

Schema: <not required>

Username: <db user>

 

Password:  <password for db user >

JDBC_URL: jdbc:microsoft:sqlserver://<;host>:1433

JDBC Driver: com.microsoft.jdbc.sqlserver.SQLServerDriver

 

JNDI LOCATIONS SECTION

Location: jdbc/SQLServerDS

Transactional (XA) Location: <optional>

EJB Location: <optional>

 

 

CONNECTION ATTRIBUTES SECTION

Connection Retry Interval (secs): 30

Max Connection Attempts: 5

 

Maximum Open Connections: 30 <depends on load and application>

Minimum Open Connections: 0   <This will actually open 5 connections>

Wait for Free Connection Timeout (secs): 30

 

PROPERTIES SECTION (Add the following properties)

 

Name: databaseName   Value: <database name i.e. NorthWinds>

Name: loginTimeout   Value: <value in seconds>

Name: selectMethod   Value: cursor

Name: serverName     Value: <name of DB host or IP address>

 

data-sources.xml Method

The data source can also be created manually by adding it to a data-sources.xml and placing the file in <OracleAS 10g HOME>j2ee<OC4J INSTANCE HOME>config directory.  The following is an example of a data-sources.xml file:

 

 

<?xml version = '1.0' standalone = 'yes'?>

<!DOCTYPE data-sources PUBLIC "Orion data-sources" "http://xmlns.oracle.com/ias/dtds/data-sources-9_04.dtd">

<data-sources>

<data-source location="jdbc/SQLServerDS"

             class="com.microsoft.jdbcx.sqlserver.SQLServerDataSource"

             password="yourpassword"

             max-connect-attempts="10"

             connection-driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"

             username="jbennett"

             min-connections="0"

             max-connections="50"

             url="jdbc:microsoft:sqlserver://localhost:1433" name="SQLServerDS">

    <property name="databaseName" value="JasonTestDB"/>

    <property name="loginTimeout" value="3000"/>

    <property name="selectMethod" value="cursor"/>

    <property name="serverName" value="tst-jason"/>

 </data-source>

</data-sources>

 

Some Sample Code to Get you Started

The following code sample demonstrates how to obtain a JNDI reference to your server side SQL Server JDBC data source.

 

package test.sqlserver.connect;

 

import javax.sql.*;

import java.sql.*;

import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;

import com.microsoft.jdbc.sqlserver.SQLServerConnection;

 

import javax.naming.*;

 

public class ConnectionTest

{

 

   private   DataSource ds = null;

   protected Connection conn = null; 

   protected CallableStatement cs = null;

   protected Statement stmt = null; 

   protected ResultSet rs = null;

   private   InitialContext context = null;  

  

  public ConnectionTest()

  {

     try

     {

       context = new InitialContext();

       ds = (DataSource)context.lookup("jdbc/SQLServerDS");

      

     }catch(Exception e)

     {

       System.out.println("Error getting SQLServer connection: "+e.getMessage());

     }

  }

 

  public String getNames() throws SQLException

  {

     String v_return = "no data";

     try

     {

       conn = ds.getConnection();

      

       stmt = conn.createStatement();

       

      

       ResultSet rs = stmt.executeQuery("select <somecolumn> from <user>.person where <criteria>");

      

       while(rs.next()){

          v_return = rs.getString(1);

       }

      

       return v_return;

     }catch(Exception e)

     {

       System.out.println("SQL error: "+e.getMessage());

     }finally

     {

       conn.close();

     }

    

     return v_return;

  }

}


7:58:22 PM    

Click here to visit the Radio UserLand website. © Copyright 2008Jason Bennett.
Last update: 8/28/2008; 9:43:58 PM.

May 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          
Apr   Aug