jQuery in Action, 2nd edition*
The moose likes Websphere and the fly likes Can I connect to database  at runtime from WSAD5.0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Websphere
Bookmark "Can I connect to database  at runtime from WSAD5.0?" Watch "Can I connect to database  at runtime from WSAD5.0?" New topic
Author

Can I connect to database at runtime from WSAD5.0?

Vidya Ram
Ranch Hand

Joined: Apr 22, 2003
Posts: 62
Can I get a JDBC connection to any database (say DB2) at runtime in WSAD5.0? I mean, can I have code like this...
try
{
DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());
Connection con= DriverManager.getConnection("jdbc b2 bname","userid","password");
}
catch(Exception ex){}
Though the DB2 database's db2java.zip is added in the WSAD's classpath, I get an error that "COM cannot be resolved or is not a type".
Can anybody help me?
TIA,
Vidya.
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
When you say the DB2 zippy is added to WSAD's classpath, what exactly do you mean? Is is part of the project's build path, server classpath, or what?


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Vidya Ram
Ranch Hand

Joined: Apr 22, 2003
Posts: 62
I mean the project's build path set in
"Window->preferences->Java->Classpath variables" and also I created a new test environment server and configured it to have db2java.zip in its path.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Adding it to the classpath variables doesn't add it to WSAD's classpath. You still need to add that variable to the classpath for whatever project is trying to access the DB (if it is not an EJB project) or check that the project is set to use your new server (if it is an EJB project.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vidya Ram
Ranch Hand

Joined: Apr 22, 2003
Posts: 62
Please let me know how do I add that variable to the classpath for the project that is trying to access the DB (for a non EJB project).
For my EJB project, I have set the project to use my new server anyway, but still I have the same problem.
Thanks a lot.
Vidya.

Originally posted by Jeanne Boyarsky:
Adding it to the classpath variables doesn't add it to WSAD's classpath. You still need to add that variable to the classpath for whatever project is trying to access the DB (if it is not an EJB project) or check that the project is set to use your new server (if it is an EJB project.)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Several things to point out here.
Firstly, the recommended way to connect to a database in an application server is via a DataSource. You register the DataSource in the Server Configuration, and it gets bound JNDI. In your code you get the DataSource from JNDI then ask it for a Connection. This is preferred since it allows the application server to manage Connections in all applications running in the application server. This means hungry applications won't choke other apps or the database by opening too many Connections, and the Application serever can provide the Connections via a Connection Pool to greatly improve performance. There is a bit of a trick to authenticating DataSources in WSAD5/WAS5, but I'll find a thread for that in a minute.
Looking at your sample code, this is not the recommended way to directly access a DRiver either. if you call Class.forName( drive name ), the Driver registers itself with the Driver manager when it is loaded. You don't have to and should not register it manually, since it registers the Driver twice and can cause errors.
The third point is that application servers ignore the system classpath. This allows them to provide a custom classpath for each application running inside it. You need to add the driver jar to the application classpath in the app serever settings, but setting it up as a DataSource will take care of this for you.
Now I'll go look for the 'DataSource in WSAD 5' thread...
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Check out this thread: How to define a datasource to DB2 in WSAD 5?
Dave
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

In the event you still need to access the DB2 packages after doing what David said (like for the DB2Blob class):
-Right Click the project
-Properties
-Build Path
-Add variable
Vidya Ram
Ranch Hand

Joined: Apr 22, 2003
Posts: 62
Thank you very much Dave and Jeane. Some more doubts...
1. I used the DataSource now. I used the statement
Context ctx = new InitialContext();
DataSource ds= (DataSource)ctx.lookup("java:comp/env/jdbc/ds1");
Then I got an error saying that "Name comp/env/jdbc not found in context "java:"
2. Then I tried
DataSource ds= (DataSource)ctx.lookup("jdbc/ds1");
Now, it got the connection and also a warning that
"Resource reference jdbc/ds1 could not be located, so default values of the following are used: [Resource-ref settings]".
3. Where can I see this Resource-ref setting? I searched in all xml files that is generated for the server but couldn't find the Resource-ref tag.
Please let me know where is that defined? And also what exact name to look up.
4. This is my first server-side project in my life and have no guidance...could you also suggest me some good book or link where I can get the details about all these techniques and in particular the implementation in WSAD?
Thanks,
Vidya.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can I connect to database at runtime from WSAD5.0?
 
Similar Threads
Problem with the JDBC driver.
WebSphere Studio JDBC Connection Problem
WebSphere Studio JDBC Connection Problem
SQLSTATE=08S01
DB2 JDBC connection