aspose file tools*
The moose likes JDBC and the fly likes datasource question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "datasource question" Watch "datasource question" New topic
Author

datasource question

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Suppose I create a datasource and a JNDI name to access a database and I set up this in my application server websphere. In order for my java code to use this data source, do I have to deploy my java code to the application server ? I know if I do that that should work. But what if I just want to test some standalone java code which is not j2ee application ? for example I just want to test a select statement and print it on console, how can this code use the data source ? I assume the datasource is created on the application server so how can the standalone code access it ?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi
You can instantiate the InitialContext using a Properties object. You need to set few properties for getting the Context of the App server.
These are
1) InitialContextFactory Name
2) Provider URL
You might optionally need to pass the username/password as well.
Please check with your vendor documentation for the appropriate values of these properties.
Regards,
Amit


Regards,
Amit
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Originally posted by amit punekar:
Hi
You can instantiate the InitialContext using a Properties object. You need to set few properties for getting the Context of the App server.
These are
1) InitialContextFactory Name
2) Provider URL
You might optionally need to pass the username/password as well.
Please check with your vendor documentation for the appropriate values of these properties.
Regards,
Amit


Thanks Amit. So you think it is possible that I can isolate my java code from application server and it can still utilize the data source I set up on the server, correct ? That's my whole point.

secondly, why do I need user_id/password (I assume you referred to the id/pwd of database) ? I think if we use datasource the code only needs the JNDI name. Is that right ?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Ben,
I've seen some drivers that require a userid being passed to the datasource from the code. The password definitely shouldn't be required in the code.


[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
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi Ben,
I have used weblogic app server to access the data source from stand alone client where if I remember correctly I had passed username/pwd for connecting to the weblogic. And not for the Database. You are right as we have already provided credentials while creating datasource we should not be passing them again. But as Jeanne suggested there might be some special cases.
Also one more thing to note here that I could not get such thing running while using IBM Websphere server. If you run the stand alone client from within RAD as IBM Client app then it runs but from command prompt I never had luck. I tried setting number of Jars in the classpath but could not run it successfully.

Regards,
amit
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
Thanks Amit. If you don't mind, could you share with us the few lines of your code that "connect to weblogic and access its datasource from outside of weblogic" ? I really have no clue how to do that. I never done that and can't find from google. Maybe everybody assumes the datasource is accessed from inside the server.

Thanks.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi Ben,
Here is the example snippet from BEA site.


And this is the link BEA Link - WLInitialContextFactory

Regds,
Amit
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: datasource question