JDBCJournalSchemaGenerator code example

The JDBCJournalSchemaGenerator is normally used by a database administration application that creates and modifies the tables needed by the applications using the journal. It can, then, be considered independent from the application that is requesting the journal services. Before requesting the database connection, the application must register and load the specific JDBC driver needed to work with the database, according to the database environment, as explained in Accessing a relational database using the JDBC interface in the Database Services documentation. The application can then request a connection to the database using the following method:
JDBCJournalSchemaGenerator jsg=new JDBCJournalSchemaGenerator();
Properties env = new Properties();  
env.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
    "com.ibm.websphere.naming.WsnInitialContextFactory");
javax.naming.Context ctx = new InitialContext(env);
DataSource ds = (DataSource)ctx.lookup("jdbc/ej");
jsg.databaseConnection = ds.getConnection(dbUserID, dbPassword);

The aUser and aPassword arguments are the userid and password to log on to the database.

The following is a sample of what this database administration application can do. The application is a runable JournalSchemaManagement class that creates the journal tables for two entities, UserA and UserB, with three generations and three table columns: BRANCHNUMBER CHAR(4), AGREEMENTNUMBER INTEGER, and DUEDATE DATE.

This sample and the following database access samples are working with a remote DB2(R) server listening on TCP/IP port 8888. Because the sample application works with a remote database, the application has to register and load the specific JDBC driver before connecting to the database. This sample shows the JDBC DB2 Net Driver, which enables the application to work with a DBMS installed on a remote workstation.

import java.util.Enumeration;
import java.sql.*;
import com.ibm.dse.base.*;
import com.ibm.dse.services.jdbc.*;

public class JournalSchemaManagement { 

public static Connection connection = null;
public static JDBCJournalSchemaGenerator jsg = null;

/**
* This method creates the journal tables using the JDBCJournalSchemaGenerator class
*/
public static void main(String args[]) { 

try { 

  // 1) Load the JDBC DB2 driver 
  Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();

  // 2) Instantiate JDBCJournalSchemaGenerator...
  System.out.println(">>> Instantiating JDBCJournalGenerator...");
  jsg = new JDBCJournalSchemaGenerator();

  // 3) Request a database connection
  jsg.connect("jdbc:db2://myhsn:8888/myDbn", "MYUSER",
  "MYPASS");

} catch (DSEException e ) {System.out.println(e.getMessage());
  return;
} 

// 3) Generate all Tables...
String myJournalSchemaName="mySchema";
Vector entities = new Vector();
entities.addElement( new String ("UserA"));
entities.addElement( new String ("UserB"));
String tableDefinition = "BRANCHNUMBER CHAR(4), AGREEMENTNUMBER INTEGER, DUEDATE
DATE";
try { 
  System.out.println(">>> Generating Database Schema...");
  jsg.generateSchema(entities, 3, tableDefinition, myJournalSchemaName);
  System.out.println(">>> Disconnecting from the database...");
  jsg.disconnect();
} catch (DSEInvalidArgumentException e) { 
  System.out.println(e.getMessage());
  return;
} catch (DSEInvalidRequestException e) { 
  System.out.println(e.getMessage());
} catch (DSESQLException e) { 
  System.out.println(e.getMessage());
  return;
} catch (DSEInternalErrorException e) { 
  System.out.println(e.getMessage());
  return;
}
}