Before using JDBC to access data in a server database file, you need to register the JDBC driver for the IBM 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:
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:
"-Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver"
To load the Toolbox for Java JDBC driver, add the following to the Java program before the first JDBC call:
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
The 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 Toolbox JDBC driver by using the following:
java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());
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 managing connections for information on managing connections.
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:
"jdbc:as400://systemName/defaultSchema;listOfProperties"
Note: Either systemName or defaultSchema can be omitted from the URL.
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 the
AS400 class and
J2SDK, v1.4 Security
Documentation
.
Examples: Using the JDBC driver to connect to a server
Example 1: Using a URL in which a system name is not specified. This will result in the user being prompted to type in the name of the system to which the user wants to connect.
"jdbc:as400:"
Example 2: Connecting to the server database; no default schema or properties specified.
// Connect to system 'mySystem'. No // default schema or properties are // specified. Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
Example 3: Connecting to the server database; default schema specified.
// Connect to system 'mySys2'. The // default schema 'myschema' is // specified. Connection c2 = DriverManager.getConnection("jdbc:as400://mySys2/mySchema");
Example 4: Connecting to the server database; properties are specified using java.util.Properties. 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 JDBC properties for a list of supported properties.
For example, to specify properties using the Properties interface, use the following code as an example:
// 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);
Example 5: Connecting to the server database; properties are specified using a uniform resource locator (URL).
// 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");
Example 6: Connecting to the server database; user ID and password are specified.
// 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");
Example 7: Disconnecting from the database. Use the close() method on the Connecting object to disconnect from the server. To close the connection that is created in the above example, use the following statement:
c.close();