ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/db2drivr.htm

196 lines
12 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="concept" />
<meta name="DC.Title" content="DriverManager" />
<meta name="abstract" content="DriverManager is a static class in the Java 2 Software Development Kit (J2SDK). DriverManager manages the set of Java Database Connectivity (JDBC) drivers that are available for an application to use." />
<meta name="description" content="DriverManager is a static class in the Java 2 Software Development Kit (J2SDK). DriverManager manages the set of Java Database Connectivity (JDBC) drivers that are available for an application to use." />
<meta name="DC.Relation" scheme="URI" content="connects.htm" />
<meta name="DC.Relation" scheme="URI" content="conprop.htm" />
<meta name="DC.Relation" scheme="URI" content="udbdatsr.htm" />
<meta name="DC.Relation" scheme="URI" content="dasrprop.htm" />
<meta name="DC.Relation" scheme="URI" content="otherdts.htm" />
<meta name="DC.Relation" scheme="URI" content="getconnections.htm" />
<meta name="DC.Relation" scheme="URI" content="accesspropertytest.htm" />
<meta name="DC.Relation" scheme="URI" content="invalidconnect.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="db2drivr" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>DriverManager</title>
</head>
<body id="db2drivr"><a name="db2drivr"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">DriverManager</h1>
<div><p>DriverManager is a static class in the Java™ 2 Software Development Kit (J2SDK).
DriverManager manages the set of Java Database Connectivity (JDBC) drivers
that are available for an application to use. </p>
<p>Applications can use multiple JDBC drivers concurrently if necessary. Each
application specifies a JDBC driver by using a Uniform Resource Locator (URL).
By passing a URL for a specific JDBC driver to the DriverManager, the application
informs the DriverManager about which type of JDBC connection should be returned
to the application.</p>
<p>Before this can be done, the DriverManager must be made aware of the available
JDBC drivers so it can hand out connections. By making a call to the Class.forName
method, it loads a class into the running Java virtual machine (JVM) based on its
string name that is passed into the method. The following is an example of
the class.forName method being used to load the native JDBC driver:</p>
<p><strong>Example:</strong> Load the native JDBC driver</p>
<div class="note"><span class="notetitle">Note:</span> By using the
code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<blockquote><pre>// Load the native JDBC driver into the DriverManager to make it
// available for getConnection requests.
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");</pre>
</blockquote>
<p>JDBC drivers are designed to tell the DriverManager about themselves automatically
when their driver implementation class loads. Once the line of code previously
mentioned has been processed, the native JDBC driver is available for the
DriverManager with which to work. The following line of code requests a Connection
object using the native JDBC URL:</p>
<p><strong>Example:</strong> Request a Connection object</p>
<p><strong>Note:</strong> Read the <a href="codedisclaimer.htm">Code example disclaimer</a> for
important legal information.</p>
<blockquote><pre>// Get a connection that uses the native JDBC driver.
Connection c = DriverManager.getConnection("jdbc:db2:*local");</pre>
</blockquote>
<p>The simplest form of JDBC URL is a list of three values that are separated
by colons. The first value in the list represents the protocol which is always <samp class="codeph">jdbc</samp> for
JDBC URLs. The second value is the subprotocol and <samp class="codeph">db2</samp> or <samp class="codeph">db2iSeries</samp> is
used to specifiy the native JDBC driver. The third value is the system name
to establish the connection to a specific system. There are two special values
that can be used to connect to the local database. They are *LOCAL and localhost
(both are case insensitive). A specific system name can also be provided as
follows:</p>
<blockquote><pre>Connection c =
DriverManager.getConnection("jdbc:db2:rchasmop");</pre>
</blockquote>
<p>This creates a connection to the rchasmop system. If the system to which
you are trying to connect is a remote system (for example, through the Distributed
Relational Database Architecture™), the system name from the relational
database directory must be used.</p>
<div class="note"><span class="notetitle">Note:</span> When not specified, the user ID and password currently used to sign
in are also used to establish the connection to the database.</div>
<div class="note"><span class="notetitle">Note:</span> The IBM<sup>®</sup> DB2<sup>®</sup> JDBC
Universal driver also uses the db2 subprotocol. To assure that the native
JDBC driver will handle the URL, applications need to use the jdbc:db2iSeries:xxxx
URL instead of the jdbc:db2:xxxx URL. If the application does not want the
native driver to accept URLS with the db2 subprotocol, then the application
should load the class com.ibm.db2.jdbc.app.DB2iSeriesDriver, instead of com.ibm.db2.jdbc.app.DB2Driver.
When this class is loaded, the native driver no longer handles URLs containing
the db2 subprotocol.</div>
<div class="section"><h4 class="sectiontitle">Properties</h4><p>The DriverManager.getConnection method
takes a single string URL indicated previously and is only one of the methods
on DriverManager to obtain a Connection object. There is also another version
of the DriverManager.getConnection method that takes a user ID and password.
The following is an example of this version:</p>
<p><strong>Example</strong>: DriverManager.getConnection
method taking a user ID and password</p>
<div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code
example disclaimer</a> for important legal information.</div>
<blockquote><pre>// Get a connection that uses the native JDBC driver.
Connection c = DriverManager.getConnection("jdbc:db2:*local", "cujo", "newtiger");</pre>
</blockquote>
<p>The
line of code attempts to connect to the local database as user cujo with password
newtiger no matter who is running the application. There is also a version
of the DriverManager.getConnection method that takes a java.util.Properties
object to allow further customization. The following is an example:</p>
<p><strong>Example:</strong> DriverManager.getConnection
method taking a java.util.Properties object</p>
<blockquote><pre>// Get a connection that uses the native JDBC driver.
Properties prop = new java.util.Properties();
prop.put("user", "cujo");
prop.put("password","newtiger");
Connection c = DriverManager.getConnection("jdbc:db2:*local", prop);</pre>
</blockquote>
<p>The
code is functionally equivalent to the version previously mentioned that passed
the user ID and password as parameters.</p>
<p>Refer to <a href="conprop.htm">Connection
properties</a> for a complete list of connection properties for the native
JDBC driver.</p>
</div>
<div class="section"><h4 class="sectiontitle">URL properties</h4><p>Another way to specify properties
is to place them in a list on the URL object itself. Each property in the
list is separated by a semi-colon and the list must be of the form <samp class="codeph">property
name=property value</samp>. This is just a shortcut and does not significantly
change the way processing is performed as the following example shows:</p>
<p><strong>Example:</strong> Specify
URL properties</p>
<blockquote><pre>// Get a connection that uses the native JDBC driver.
Connection c = DriverManager.getConnection("jdbc:db2:*local;user=cujo;password=newtiger");</pre>
</blockquote>
<p>The
code is again functionally equivalent to the examples mentioned previously.</p>
<p>If
a property value is specified in both a properties object and on the URL object,
the URL version takes precedence over the version in the properties object.
The following is an example:</p>
<p><strong>Example:</strong> URL properties</p>
<p><strong>Note:</strong> Read
the <a href="codedisclaimer.htm">Code example disclaimer</a> for important
legal information.</p>
<blockquote><pre>// Get a connection that uses the native JDBC driver.
Properties prop = new java.util.Properties();
prop.put("user", "someone");
prop.put("password","something");
Connection c = DriverManager.getConnection("jdbc:db2:*local;user=cujo;password=newtiger",
prop);</pre>
</blockquote>
<p>The example uses the user ID and password from the
URL string instead of the version in the Properties object. This ends up being
functionally equivalent to the code previously mentioned.</p>
<p>See the following
examples for more information:</p>
<ul><li><a href="getconnections.htm">Use native JDBC and IBM Toolbox for Java JDBC
concurrently</a></li>
<li><a href="accesspropertytest.htm">Access property</a></li>
<li><a href="invalidconnect.htm">Invalid user ID and password</a></li>
</ul>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="getconnections.htm">Example: Use native JDBC and IBM Toolbox for Java JDBC concurrently</a></strong><br />
This is an example of how to use the native JDBC connection and
the IBM Toolbox
for Java JDBC
connection in a program.</li>
<li class="ulchildlink"><strong><a href="accesspropertytest.htm">Example: Access property</a></strong><br />
This is an example of how to use the Access property.</li>
<li class="ulchildlink"><strong><a href="invalidconnect.htm">Example: Invalid user ID and password</a></strong><br />
This is an example of how to use the Connection property in SQL naming mode.</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="connects.htm" title="The Connection object represents a connection to a data source in Java Database Connectivity (JDBC). It is through Connection objects that Statement objects are created for processing SQL statements against the database. An application program can have multiple connections at one time. These Connection objects can all connect to the same database or connect to different databases.">Connections</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="udbdatsr.htm" title="DataSource interfaces were designed to allow additional flexibility in using Java Database Connectivity (JDBC) drivers.">Use DataSources with UDBDataSource</a></div>
<div><a href="otherdts.htm" title="There are two implementations of the DataSource interface that are included with the native JDBC driver. These DataSource implementations should be considered deprecated. While you can still use them, they are not enhanced with future improvements; for example, robust connection and statement pooling are not added to these implementations. These implementations exist until you adopt the UDBDataSource interface and its related functions.">Other DataSource implementations</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="conprop.htm" title="This table contains valid JDBC driver connection properties, their values, and their descriptions.">Connection properties</a></div>
<div><a href="dasrprop.htm" title="This table contains valid data source properties, their values, and their descriptions.">DataSource properties</a></div>
</div>
</div>
</body>
</html>