How to access DB2 using DataSource?

Z

zalek

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_passw);

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
 
A

Arne Vajhøj

zalek said:
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_passw);

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
 
C

Cesar

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_passw);

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top