Begin changeAS400JDBCDataSource

The AS400JDBCDataSource class represents a factory for AS/400 database connections.

You can register AS400JDBCDataSource objects by using a Java Naming and Directory Interface (JNDI) service provider. For more information on JNDI service providers, see AS/400 Toolbox for Java reference links.

Examples

The examples below demonstrate ways to create and use AS400JDBCDataSource objects. The last two examples show how to register an as400JDBCDataSource object with JNDI and then use the object returned from JNDI to obtain a database connection. Notice that even when using different JNDI service providers, the code is very similar.

Example: Creating an AS400JDBCDataSource object

The following example shows you how to create an AS400JDBCDataSource object and connect it to a database:

      // Create a data source for making the connection.
       AS400JDBCDataSource datasource = new AS400JDBCDataSource("myAS400");
       datasource.setUser("myUser");
       datasource.setPassword("MYPWD");

       // Create a database connection to the AS/400.
       Connection connection = datasource.getConnection();

Example: Using JNDI service provider classes to store an AS400JDBCDataSource object

The follwoing example shows how you can use JNDI service provider classes to store a DataSource object directly to the IFS file system on the server:

       // Create a data source to the AS/400 database.
       AS400JDBCDataSource dataSource = new AS400JDBCDataSource();
       dataSource.setServerName("myAS400");
       dataSource.setDatabaseName("myAS400 Database");

       // Register the datasource with the Java Naming and Directory Interface (JNDI).
       Hashtable env = new Hashtable();
       env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
       Context context = new InitialContext(env);
       context.bind("jdbc/customer", dataSource);

       // Return an AS400JDBCDataSource object from JNDI and get a connection.
       AS400JDBCDataSource datasource = (AS400JDBCDataSource) context.lookup("jdbc/customer");
       Connection connection = datasource.getConnection("myUser", "MYPWD");

Example: Using AS400JDBCDataSource objects and IBM SecureWay Directory classes with a Lightweight Directory Access Protocol (LDAP) directory server

The following examples shows how you can use IBM SecureWay Directory classes to store an object to a Lightweight Directory Access Protocol (LDAP) directory server:

       // Create a data source to the AS/400 database.
       AS400JDBCDataSource dataSource = new AS400JDBCDataSource();
       dataSource.setServerName("myAS400");
       dataSource.setDatabaseName("myAS400 Database");

       // Register the datasource with the Java Naming and Directory Interface (JNDI).
       Hashtable env = new Hashtable();
       env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.jndi.LDAPCtxFactory");
       Context context = new InitialContext(env);
       context.bind("cn=myDatasource, cn=myUsers, ou=myLocation,o=myCompany,c=myCountry", dataSource);

       // Return an AS400JDBCDataSource object from JNDI and get a connection.
       AS400JDBCDataSource datasource = (AS400JDBCDataSource) context.lookup("cn=myDatasource,
            cn=myUsers, ou=myLocation,o=myCompany,c=myCountry");
       Connection connection = datasource.getConnection("myUser", "MYPWD");
End change