Ability to connect to databases within a JDBC/SQLJ application

[Note]
  • [INFOCENTER_V9] URL format for IBM Data Server Driver for JDBC and SQLJ type 4 connectivity

  • [INFOCENTER_V9] URL format for IBM Data Server Driver for JDBC and SQLJ type 2 connectivity

Typically, a JDBC application connects to a target data source using one of two mechanisms:

DataSource Overview

A DataSource object is the representation of a data source in the Java programming language. In basic terms, a data source is a facility for storing data. It can be as sophisticated as a complex database for a large corporation or as simple as a file with rows and columns. A data source can reside on a remote server, or it can be on a local desktop machine. Applications access a data source using a connection, and a DataSource object can be thought of as a factory for connections to the particular data source that the DataSource instance represents.

The following code fragment shows application code that uses AcmeDS logical name to connect to the database that data source represents:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/AcmeDB");
Connection con = ds.getConnection("zaikin", "passwd");
con.setAutoCommit(false);
					
The first two lines use JNDI API; the third line uses DataSource API. After the first line creates an instance of javax.naming.Context for the initial naming context, the second line calls the method lookup on it to get the DataSource object associated with jdbc/AcmeDB. However, the return value for the method lookup is a reference to a Java Object, the most generic of objects, so it must be cast to the more narrow DataSource before it can be assigned to the DataSource variable ds.

Using the DriverManager Class

The DriverManager class works with the Driver interface to manage the set of drivers available to a JDBC client. When the client requests a connection and provides a URL, the DriverManager is responsible for finding a driver that recognizes the URL and for using it to connect to the corresponding data source. Connection URLs have the following form:


jdbc:derby:<dbName>[propertyList]

					

The dbName portion of the URL identifies a specific database. A database can be in one of many locations: in the current working directory, on the classpath, in a JAR file, in a specific Java DB database home directory, or in an absolute location on your file system.

If you are using a vendor-specific driver, such as Oracle, the documentation will tell you what subprotocol to use, that is, what to put after jdbc: in the JDBC URL. For example, if the driver developer has registered the name OracleDriver as the subprotocol, the first and second parts of the JDBC URL will be jdbc.driver.OracleDriver. The driver documentation will also give you guidelines for the rest of the JDBC URL. This last part of the JDBC URL supplies information for identifying the data source.

The getConnection method establishes a connection:

Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
					

In place of "myLogin" you insert the name you use to log in to the DBMS; in place of "myPassword" you insert your password for the DBMS. So, if you log in to your DBMS with a login name of "zaikin" and a password of "passwd", just these two lines of code will establish a connection:

String url = "jdbc:derby:Fred";
Connection con = DriverManager.getConnection(url, "zaikin", "passwd");
					

If one of the drivers you loaded recognizes the JDBC URL supplied to the method DriverManager.getConnection, that driver establishes a connection to the DBMS specified in the JDBC URL. The DriverManager class, true to its name, manages all of the details of establishing the connection for you behind the scenes. Unless you are writing a driver, you probably won't use any of the methods in the interface Driver, and the only DriverManager method you really need to know is DriverManager.getConnection

The connection returned by the method DriverManager.getConnection is an open connection you can use to create JDBC statements that pass your SQL statements to the DBMS.

URL format for IBM Data Server Driver for JDBC and SQLJ type 4 connectivity

If you are using type 4 connectivity in your JDBC application, and you are making a connection using the DriverManager class, you need to specify a URL in the DriverManager.getConnection call that indicates type 4 connectivity.

IBM Data Server Driver for JDBC and SQLJ type 4 connectivity URL syntax:


>>-+-jdbc:db2:------+--//--server--+---------+--/--database----->
   +-jdbc:db2j:net:-+              '-:--port-'                
   '-jdbc:ids:------'                                         

>--+------------------------------+----------------------------><
   |    .-----------------------. |   
   |    V                       | |   
   '-:----property--=--value--;-+-'   

					

The parts of the URL have the following meanings:

URL format for IBM Data Server Driver for JDBC and SQLJ type 2 connectivity

If you are using type 2 connectivity in your JDBC application, and you are making a connection using the DriverManager class, you need to specify a URL in the DriverManager.getConnection call that indicates type 2 connectivity.

IBM Data Server Driver for JDBC and SQLJ type 2 connectivity URL syntax:


>>---+-jdbc--:--db2--:--database----------+--------------------->
     +-jdbc--:--db2os390--:--database-----+     
     +-jdbc--:--db2os390sqlj--:--database-+     
     +-jdbc--:--default--:--connection----+     
     +-jdbc--:--db2os390------------------+     
     '-jdbc--:--db2os390sqlj--------------'     

>--+------------------------------+----------------------------><
   |    .-----------------------. |   
   |    V                       | |   
   '-:----property--=--value--;-+-'   

					

The parts of the URL have the following meanings:

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz