![]() | |
|
Typically, a JDBC application connects to a target data source using one of two mechanisms:
DataSource: This interface is preferred over DriverManager because it allows details about the underlying data source to be transparent to your application. A DataSource object's properties are set so that it represents a particular data source.
DriverManager: This fully implemented class requires an application to load a specific driver, using a hardcoded URL. As part of its initialization, the DriverManager class attempts to load the driver classes referenced in the jdbc.drivers system property. This allows you to customize the JDBC Drivers used by your applications.
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:
jdbc:db2
Indicates that the connection is to a DB2® for z/OS®, DB2 Database for Linux®, UNIX®, and Windows®.
jdbc:db2j:net
Indicates that the connection is to a remote IBM Cloudscape server.
jdbc:ids
Indicates that the connection is to an IDS data source. jdbc:informix-sqli: also indicates that the connection is to an IDS data source, but jdbc:ids: should be used.
server
The domain name or IP address of the data source.
port
The TCP/IP server port number that is assigned to the data source. This is an integer between 0 and 65535. The default is 446.
database
A name for the data source.
property=value;
A property and its value for the JDBC connection. You can specify one or more property and value pairs. Each property and value pair, including the last one, must end with a semicolon (;). Do not include spaces or other white space characters anywhere within the list of property and value strings.
Some properties with an int data type have predefined constant field values. You must resolve constant field values to their integer values before you can use those values in the url parameter. For example, you cannot use com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL in a url parameter. However, you can build a URL string that includes com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL, and assign the URL string to a String variable. Then you can use the String variable in the url parameter:
String url = "jdbc:db2://sysmvs1.stl.ibm.com:5021" + "user=dbadm;password=dbadm;" + "traceLevel=" + (com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL) + ";"; Connection con = java.sql.DriverManager.getConnection(url);
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:
jdbc:db2 or jdbc:db2os390 or jdbc:db2os390sqlj
Indicates that the connection is to a DB2® for z/OS® or DB2 Database for Linux®, UNIX®, and Windows® server. jdbc:db2os390: and jdbc:db2os390sqlj: are for compatibility of programs that were written for older drivers, and apply to IBM® Data Server Driver for JDBC and SQLJ type 2 connectivity to DB2 for z/OS only.
jdbc:default:connection
Indicates that the URL is for a connection to the local subsystem through a DB2 thread that is controlled by CICS®, IMS™, or the Java™ stored procedure environment.
database
A name for the database server.
property=value;
A property and its value for the JDBC connection. You can specify one or more property and value pairs. Each property and value pair, including the last one, must end with a semicolon (;). Do not include spaces or other white space characters anywhere within the list of property and value strings.
Some properties with an int data type have predefined constant field values. You must resolve constant field values to their integer values before you can use those values in the url parameter. For example, you cannot use com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL in a url parameter. However, you can build a URL string that includes com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL, and assign the URL string to a String variable. Then you can use the String variable in the url parameter:
String url = "jdbc:db2:STLEC1" + "user=dbadm;password=dbadm;" + "traceLevel=" + (com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL) + ";"; Connection con = java.sql.DriverManager.getConnection(url);
![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |