Jdbc connection in EJB using wsad 5.0

Hi,

I want to develop a small application using Ejbs by using oracle 9i as D/b, wsad 5.0 as app server . The problem is i am not able to connect to oracle database in WSAD. What i am doing is i have installed Oracle 9i in my system and has given the Global database name as "samp" while installation . I open my SQL plus with username scott and password tiger and i am able to do all my sql queries successfully.

Now coming to WSAD,

while creating a JDBC connection using Oracle 9i driver,

i ve opened in Data perspective and in that go to DB Servers -> Right click -> New Connection

There a window is opened for Database Connection.

We need to fill the fields there.

I have given samp as Global d/b name while installing Oracle 9i .

In the window , the feilds are

Connection Name : conn

Database Name : samp

user id : scott

password : tiger

D/b vendor type: Oracle 9i

Jdbc Driver : Oracle Thin Driver

Host : 127.0.0.1

port No: 1521

class location : c:\oracle\ora90\jdbc\lib\classes12.zip

connection url : jdbc:oracle:thin:@127.0.0.1:1521:samp

the class location and connection url are automatically coming.

and please check whether all fields are correct or not

Is this the correct way.

Next in code if i want to connect to database should i use connection establish commands again or i can directly use create statement or prepare statement.

Please reply.

Thanks

[1531 byte] By [mynaa] at [2007-10-2 15:40:51]
# 1
Hi,This is not what you should be doing. You must create a datasource from the server perspective and then you this datasource for getting connections to your database.I hope this helps you.VJ
jain_vishal_aa at 2007-7-13 15:26:16 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi,

Thanks for reply

I have tried in that way also. What i m doing is first i m starting the server and right click on it -> Clicking on create tables and databases.

then it is giving a message like ... operation cant be performed.

or sometimes the option create tables and databases is not enabled. it is in disabled mode. please give me a sequence of steps inorder to connect to database.

Thanks

mynaa at 2007-7-13 15:26:16 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hi

1. Why do you want to create database from your WSAD? Don't you have a database already existing.

2. First create a new server in WSAD.

3. Open server configuration

4. Select datasources TAB.

5. Follow the steps.

6. Look up your datasource using JNDI.

7. Get a connection from this datasource.

HTH

VJ

jain_vishal_aa at 2007-7-13 15:26:17 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Thank U very much. I got it.
mynaa at 2007-7-13 15:26:17 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Create New Server and configure it properly

It will work

procedure is as follows:

Pls visit the following link:

http://www.webagesolutions.com/knowledgebase/waskb/waskb001/index.html

Adding a Oracle9i DataSource from WSAD5

Bibhas Bhattacharya, Web Age Solutions Inc.

Before you begin, make sure that you have Oracle installed and a database is created. In this document we will use a database called MALL.

Create a WAS V5 Server

If you don't already have a WebSphere V5 server created, do so following these steps. Switch to the Server perspective. Right click in the Server Configuration view and select New->Server and Server Configuration.

Name the server WASV5. Make sure that the Server type is set to WebSphere version 5.0->Test Environment. Click on Finish.

Add the Database User

In WSAD5, the default user ID and password to be used by a DataSource are first entered as a JAAS authentication entry.

In the Server Configuration view, double click on WASV5 to open the configuration editor. Click on the Security tab. Next to the JAAS Authentication Entries list click on Add and add the user.

Add the JDBC Driver

Still in the server configuration GUI click on the DataSource tab. You can add the DataSource at the server level or at the node level. We will add it at the server level. Make sure that the Server Settings is expanded. Next to the JDBC providers list click on Add.

Select the following options:

Database type: Oracle

JDBC provider type: Oracle JDBC Thin Driver or the XA version of it if you need two phase commit transaction.

Click on Next.

Set the name to Oracle Thin Driver.

Notice that the location of the driver's class is automatically set to ${ORACLE_JDBC_DRIVER_PATH}/classes12.zip. Here, ORACLE_JDBC_DRIVER_PATH is a node level variable. We need to make sure that the variable is pointing to the correct directory where Oracle's JDBC driver is installed. In our case, we had installed Oracle in c:\oracle. This had installed the JDBC driver class in C:/oracle/ora81/jdbc/lib/classes12.zip.

In the server configuration GUI click on the Variables tab. Under the Node settings select ORACLE_JDBC_DRIVER_PATH from the Defined variables list. Click on Edit and set the value to C:/oracle/ora81/jdbc/lib.

Add the DataSource

Click on the DataSource tab again. Select the Oracle Thin Driver you had created in the previous step. Click on Add next to the Data source defined in the JDBC provider selected above list.

Select the following options:

Select the type of JDBC Driver: Oracle JDBC Thin Driver.

Select the data source type: Unless you will be testing your application with WAS V4, select Version 5.0. You can not use a V4 DataSource from a J2EE 1.3 EJB module running in WebSphere V5.

Click on Next.

Enter these key attributes in this screen:

Name: My Oracle DataSource

JNDI Name: jdbc/MyDataSource

DataSource helper class name: com.ibm.websphere.rsadapter.OracleDataStoreHelper. Should be selected by default. The helper class is needed if you wish to access IBM extensions to JDBC. For more details search in WSAD help for "WSDataSource interface".

Component-managed authentication alias: Set this if you wish to lookup the DataSource using its global JNDI name or using the java:comp/env/ name space and have set the authentication type of the resource reference to Application. Select the JAAS entry you had created. That is, Database user.

Container-managed authentication alias: Set this if you intend to lookup the DataSource using the java:comp/env/ name space and have set the authentication type of the resource reference to Container. Select the JAAS entry you had created. That is, Database user.

Use this data source in container managed persistence (CMP): Check on if you intend to use the DataSource from CMP EJBs.

Click on Next.

You need to set these properties:

databaseName: MALL in our case.

URL: jdbc:oracle:thin:@noble.webagesolutions.com:1521:MALL. In my case the server host name is noble.webagesolutions.com. The listener port number is 1521 (usually the default in most Oracle installations).

Click on Finish.

You have finished adding the DataSource. Save the server settings by clicking Control+S. Close the server configuration GUI.

Testing the DataSource

There is no out of the box way to test the DataSource. You can create a simple Servlet and add the following code:

public void doGet(HttpServletRequest req, HttpServletResponse resp)

javax.sql.DataSource ds = null;

java.sql.Connection con = null;

java.io.PrintWriter out = resp.getWriter();

resp.setContentType("text/html");

try {

out.println("Looking up DataSource

");

javax.naming.InitialContext ctx = new javax.naming.InitialContext();

ds = (javax.sql.DataSource) ctx.lookup("jdbc/MyDataSource");

out.println("Getting connection

");

con = ds.getConnection();

con.close();

} catch (Exception e) {

e.printStackTrace(out);

}

out.println("Done

");

}

Feedback

Your e-mail:

Rate this article:

Very useful Somewhat useful Not bad Needs many corrections

Comments:

Singhbla at 2007-7-13 15:26:17 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...