151 lines
8.3 KiB
HTML
151 lines
8.3 KiB
HTML
|
<?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="reference" />
|
||
|
<meta name="DC.Title" content="Registering the JDBC driver" />
|
||
|
<meta name="abstract" content="" />
|
||
|
<meta name="description" content="" />
|
||
|
<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="jdbcdrvr" />
|
||
|
<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>Registering the JDBC driver</title>
|
||
|
</head>
|
||
|
<body id="jdbcdrvr"><a name="jdbcdrvr"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Registering the JDBC driver</h1>
|
||
|
<div><p></p>
|
||
|
<div class="section"><p>Before using JDBC to access data in a server database file, you
|
||
|
need to register the <a href="javadoc/com/ibm/as400/access/AS400JDBCDriver.html">JDBC driver</a> for the IBM<sup>®</sup> Toolbox for Java™ licensed program with the DriverManager.
|
||
|
You can register the driver either by using a Java system property or by having the Java program
|
||
|
register the driver:</p>
|
||
|
<ul><li>Register by using a system property <p>Each virtual machine has its own
|
||
|
method of setting system properties. For example, the Java command
|
||
|
from the JDK uses the -D option to set system properties. To set the driver
|
||
|
using system properties, specify the following:</p>
|
||
|
<pre> "-Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver"</pre>
|
||
|
</li>
|
||
|
<li>Register by using the Java program <p>To load the IBM Toolbox for Java JDBC
|
||
|
driver, add the following to the Java program before the first JDBC call:</p>
|
||
|
<pre> Class.forName("com.ibm.as400.access.AS400JDBCDriver");</pre>
|
||
|
<p>The IBM Toolbox
|
||
|
for Java JDBC
|
||
|
driver registers itself when it is loaded, which is the preferred way to register
|
||
|
the driver. You can also explicitly register the IBM Toolbox JDBC driver by using the following:</p>
|
||
|
<pre> java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());</pre>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<p>The IBM Toolbox
|
||
|
for Java JDBC
|
||
|
driver does not require an AS400 object as an input parameter like the other IBM Toolbox
|
||
|
for Java classes
|
||
|
that get data from a server. An AS400 object is used internally, however,
|
||
|
to manage default user and password caching. When a connection is first made
|
||
|
to the server, the user may be prompted for user ID and password. The user
|
||
|
has the option to save the user ID as the default user ID and add the password
|
||
|
to the password cache. As in the other IBM Toolbox for Java functions, if the user ID and password
|
||
|
are supplied by the Java program, the default user is not set
|
||
|
and the password is not cached. See <a href="mngcon.htm#mngcon">Managing connections</a> for
|
||
|
information on managing connections.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Using the JDBC driver to connect to a database on the server</h4><p>You
|
||
|
can use the DriverManager.getConnection() method to connect to the server
|
||
|
database. DriverManager.getConnection() takes a uniform resource locator
|
||
|
(URL) string as an argument. The JDBC driver manager attempts to locate a
|
||
|
driver that can connect to the database that is represented by the URL. When
|
||
|
using the IBM Toolbox
|
||
|
for Java driver,
|
||
|
use the following syntax for the URL:</p>
|
||
|
<pre> "jdbc:as400://systemName/defaultSchema;listOfProperties"</pre>
|
||
|
<div class="note"><span class="notetitle">Note:</span> Either
|
||
|
systemName or defaultSchema can be omitted from the URL.</div>
|
||
|
<p>To use
|
||
|
Kerberos tickets, set only the system name (and not the password) on your
|
||
|
JDBC URL object. The user identity is retrieved through the Java Generic
|
||
|
Security Services (JGSS) framework, so you also do not need to specify a
|
||
|
user on your JDBC URL. You can set only one means of authentication in an
|
||
|
AS400JDBCConnection object at a time. Setting the password clears any Kerberos
|
||
|
ticket or profile token. For more information, see <a href="as400obj.htm#as400obj">AS400 class</a> and
|
||
|
<a href="http://java.sun.com/j2se/1.4/docs/guide/security/index.html" target="_blank">J2SDK, v1.4 Security Documentation</a> <img src="www.gif" alt="Link outside information center" />.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Examples: Using the JDBC driver to connect to a server</h4><p><strong>Example:
|
||
|
Using a URL in which a system name is not specified</strong></p>
|
||
|
<p>This example
|
||
|
results in the user being prompted to type in the name of the system to which
|
||
|
he or she wants to connect.</p>
|
||
|
<pre> // Connect to unnamed system.
|
||
|
// User receives prompt to type system name.
|
||
|
Connection c = DriverManager.getConnection("jdbc:as400:");</pre>
|
||
|
<p><strong>Example:
|
||
|
Connecting to the server database; no default schema or properties specified</strong></p>
|
||
|
<pre> // Connect to system 'mySystem'. No
|
||
|
// default schema or properties are
|
||
|
// specified.
|
||
|
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");</pre>
|
||
|
<p><strong>Example:
|
||
|
Connecting to the server database; default schema specified</strong></p>
|
||
|
<pre> // Connect to system 'mySys2'. The
|
||
|
// default schema 'myschema' is
|
||
|
// specified.
|
||
|
Connection c2 = DriverManager.getConnection("jdbc:as400://mySys2/mySchema");</pre>
|
||
|
<p><strong>Example:
|
||
|
Connecting to the server database and using java.util.Properties to specify
|
||
|
properties</strong></p>
|
||
|
<p>The Java program can specify a set of JDBC
|
||
|
properties either by using the java.util.Properties interface or by specifying
|
||
|
the properties as part of the URL. See <a href="jdbcproperties.htm#jdbcproperties">IBM Toolbox for Java JDBC properties</a> for
|
||
|
a list of supported properties.</p>
|
||
|
<p>For example, to specify properties using
|
||
|
the Properties interface, use the following code as an example:</p>
|
||
|
<pre> // Create a properties object.
|
||
|
Properties p = new Properties();
|
||
|
|
||
|
// Set the properties for the
|
||
|
// connection.
|
||
|
p.put("naming", "sql");
|
||
|
p.put("errors", "full");
|
||
|
|
||
|
// Connect using the properties
|
||
|
// object.
|
||
|
Connection c = DriverManager.getConnection("jdbc:as400://mySystem",p);</pre>
|
||
|
<p><strong>Example:
|
||
|
Connecting to the server database and using a uniform resource locator (URL)
|
||
|
to specify properties</strong></p>
|
||
|
<pre> // Connect using properties. The
|
||
|
// properties are set on the URL
|
||
|
// instead of through a properties
|
||
|
// object.
|
||
|
Connection c = DriverManager.getConnection(
|
||
|
"jdbc:as400://mySystem;naming=sql;errors=full");</pre>
|
||
|
<p><strong>Example:
|
||
|
Connecting to the server database and specifying user ID and password</strong></p>
|
||
|
<pre> // Connect using properties on the
|
||
|
// URL and specifying a user ID and
|
||
|
// password
|
||
|
Connection c = DriverManager.getConnection(
|
||
|
"jdbc:as400://mySystem;naming=sql;errors=full",
|
||
|
"auser",
|
||
|
"apassword");</pre>
|
||
|
<p><strong>Example: Disconnecting
|
||
|
from the database</strong>To disconnect from the server, use the close() method
|
||
|
on the Connecting object. Use the following statement to close the connection
|
||
|
created in the previous example:</p>
|
||
|
<pre> c.close();</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|