By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

How to access DB2 using DataSource?

P: n/a
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
Aug 15 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
zalek wrote:
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
Aug 15 '08 #2

P: n/a
On 14 ago, 21:09, zalek <zalekbl...@hotmail.comwrote:
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!
Aug 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.