Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 15th, 2008, 03:15 AM
zalek
Guest
 
Posts: n/a
Default How to access DB2 using DataSource?

Hello,

I wrote a program that is working OK which access DB2 using the
following code:

String w_conn = "jdbc:db2:DB2MAIN;
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection db2Conn =
DriverManager.getConnection(w_conn,w_userid,w_pass w);

Now I am reading I can access DB2 using JDNI service where code is
like this:

Connection conn = null;
try {
Context initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup(w_service);
conn = ds.getConnection();

Which way is consider more efficient?

What is the variable w_service? I understand w_service is a String –
name of DB2 service, but what is a value of this string?

Thanks,

Zalek
  #2  
Old August 15th, 2008, 03:25 AM
=?windows-1252?Q?Arne_Vajh=F8j?=
Guest
 
Posts: n/a
Default Re: How to access DB2 using DataSource?

zalek wrote:
Quote:
I wrote a program that is working OK which access DB2 using the
following code:
>
String w_conn = "jdbc:db2:DB2MAIN;
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection db2Conn =
DriverManager.getConnection(w_conn,w_userid,w_pass w);
>
Now I am reading I can access DB2 using JDNI service where code is
like this:
>
Connection conn = null;
try {
Context initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup(w_service);
conn = ds.getConnection();
>
Which way is consider more efficient?
>
What is the variable w_service? I understand w_service is a String –
name of DB2 service, but what is a value of this string?
Lookup via JNDI is what you should use if your code is running
inside a container like WAS.

It makes it possible to use connection pools etc..

The JNDI name is the name given to the database resource
in JNDI.

Arne
  #3  
Old August 16th, 2008, 03:05 PM
Cesar
Guest
 
Posts: n/a
Default Re: How to access DB2 using DataSource?

On 14 ago, 21:09, zalek <zalekbl...@hotmail.comwrote:
Quote:
Hello,
>
I wrote a program that is working OK which *access DB2 using the
following code:
>
String w_conn = "jdbc:db2:DB2MAIN;
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection db2Conn =
DriverManager.getConnection(w_conn,w_userid,w_pass w);
>
Now I am reading I can access DB2 using JDNI service where code is
like this:
>
Connection conn = null;
try {
* * *Context initialContext = new InitialContext();
* * *DataSource ds = (DataSource)initialContext.lookup(w_service);
* * *conn = ds.getConnection();
>
Which way is consider more efficient?
>
What is the variable w_service? I understand w_service is a String –
name of DB2 service, but what is a value of this string?
>
Thanks,
>
Zalek
Hi Zalek,
Since you have access to use the datasource then i suggest you do, i
always prefer using it because is more flexible for my programing.
For example, you can change the user/password, the database name, the
server name or port number...only by modifying the DataSource
configuration without changing any line of your code.
About the w_service variable, its the jndi name of the Datasource
created in the application server. The standar for this name is "jdbc/
MyDatasource" (notice the jdbc/ prefix).


good luck!
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles