EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #32 background imageLoading...
Page #32 background image
The best way to use a DataSource object is for your system administrator to create
and manage it separately, using WebSphere Application Server or some other tool.
The program that creates and manages a DataSource object also uses the Java
Naming and Directory Interface (JNDI) to assign a logical name to the DataSource
object. The JDBC application that uses the DataSource object can then refer to the
object by its logical name, and does not need any information about the underlying
data source. In addition, your system administrator can modify the data source
attributes, and you do not need to change your application program.
To learn more about using WebSphere to deploy DataSource objects, go to this
URL on the Web:
http://www.ibm.com/software/webservers/appserv/
To learn about deploying DataSource objects yourself, see Creating and deploying
DataSource objects.
You can use the DataSource interface and the DriverManager interface in the same
application, but for maximum portability, it is recommended that you use only the
DataSource interface to obtain connections.
To obtain a connection using a DataSource object that the system administrator has
already created and assigned a logical name to, follow these steps:
1. From your system administrator, obtain the logical name of the data source to
which you need to connect.
2. Create a Context object to use in the next step. The Context interface is part of
the Java Naming and Directory Interface (JNDI), not JDBC.
3. In your application program, use JNDI to get the DataSource object that is
associated with the logical data source name.
4. Use the DataSource.getConnection method to obtain the connection.
You can use one of the following forms of the getConnection method:
getConnection();
getConnection(String user, String password);
Use the second form if you need to specify a user ID and password for the
connection that are different from the ones that were specified when the
DataSource was deployed.
Example of obtaining a connection using a DataSource object that was created by the
system administrator: In this example, the logical name of the data source that you
need to connect to is jdbc/sampledb. The numbers to the right of selected
statements correspond to the previously-described steps.
Example of creating and using a DataSource object in the same application:
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
...
Context ctx=new InitialContext(); 2
DataSource ds=(DataSource)ctx.lookup("jdbc/sampledb"); 3
Connection con=ds.getConnection(); 4
Figure 4. Obtaining a connection using a DataSource object
Figure 5. Creating and using a DataSource object in the same application
16 Application Programming Guide and Reference for Java

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability