473,408 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
Aug 15 '08 #1
2 4689
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Phil Powell | last post by:
<cfquery name="getAll" datasource="#request.dsn#"> SELECT U.userID, U.fname, U.lname, U.phone, U.lastLoggedIn, U.choiceId, U.experience, T.label AS teamLabel, R.label AS roleLabel FROM User U...
6
by: Ecohouse | last post by:
I have a computer with XP on it. I loaded Office 97 first because I needed Access 97 for some work. I then loaded Office 2000. Everything seemed to be running fine. But I have come across a few...
9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
42
by: PC Datasheet | last post by:
I have zero experience with using a SQL database for a backend and Access for a frontend. I have some questions: 1. Does an SQL database have tables? 2. How does Access connect to the data in...
2
by: | last post by:
Hello All, I am having a lot of difficulty trying to bind a templated column, that is programmatically created for a datagrid, to a datasource column. I have a datasource containing 2 columns,...
12
by: Selva Chinnasamy | last post by:
Hi I am using batch commands against Ms-Access and getting an error Message "Characters found after end of SQL statement." String Here is my sql Dim str_screens As String = "Select * from Screens...
9
by: Tim | last post by:
Hi, I hope this is an easy question for you guys... I have an application that I need to be able to switch from sqlserver to access at runtime. I can change from one sqlserver installation to...
3
by: eagleofjade | last post by:
I am trying to help a friend who is learning VB.net in school. I have done VB programming for a number of years using VB 6. He needs to open a query in an Access database that has parameters so he...
6
by: Wesley Peace | last post by:
I hate to cross post, but I've gotten no answer yet on a problem I'm having with visual studio 2008. I've created a series of forms with controls to access a Access database tables. The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.