AS400JDBCDataSource class

The AS400JDBCDataSource class represents a factory for iSeries™ database connections. The AS400JDBCConnectionPoolDataSource class represents a factory for AS400JDBCPooledConnection objects.

You can register either kind of data source object by using a Java™ Naming and Directory Interface (JNDI) service provider. For more information about JNDI service providers, see IBM® Toolbox for Java reference links.

Examples

The following examples 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 iSeries.
       Connection connection = datasource.getConnection();

Example: Creating an AS400JDBCConnectionPoolDataSource object that can be used to cache JDBC connections

The following example shows how you can use an AS400JDBCConnectionPoolDataSource to cache JDBC connections.

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

       // Get the PooledConnection.
       PooledConnection pooledConnection = datasource.getPooledConnection();

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

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

       // Create a data source to the iSeries 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 iSeries 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=myCountryRegion",
                    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=myCountryRegion");
       Connection connection = datasource.getConnection("myUser", "MYPWD");