Using the JDBC driver to connect to an AS/400 database

You can use the DriverManager.getConnection() method to connect to the AS/400 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 AS/400 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.

Examples: Using the JDBC driver to connect to an AS/400

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 AS/400 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 AS/400 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 AS/400 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 AS/400 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 AS/400 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 AS/400. To close the connection that is created in the above example, use the following statement:

     c.close();


[ Legal | AS/400 Glossary ]