The JDBCTable service works with an existing database table. Therefore, it does not manage the table's primary keys when new records are added to the database. An application using this service must set the primary key column for any record to be added, since records with duplicate primary keys are not allowed. Before requesting the database connection, the application must register and load the specific JDBC driver needed to work with the database.
The following is an example application working with the JDBC-specific implementation of a Database Table Mapping service and the XML files needed to run it. As shown in the example, the reuse of the application code with a different implementation of a table service depends on reference to the TableService interface instead of explicitly use using the JDBCTable object implementing this interface. This example is implemented for a local environment, but you can easily adapt it to a client/server environment by implementing a toolkit client operation. The client operation should start the server operation that calls for the table services. In this example, the application requests the connection to be automatically set by the service, and also lets the service commit and roll back the database changes automatically. It is assumed that the appropriate JDBC driver is loaded.
<!-- Services definition sample file. Note that no column tag is used. This means that the data field names match the column names in the database table--> <JDBCTable id="myTableName" autoCommit="true" autoConnect="true" databaseURL="jdbc:db2://myhostname:8888/mydatabasename" user="myuser" password="mypassword" table="DatabaseTable"> </JDBCTable>
The code below is a runnable class that will work with a table named DatabaseTable with the following column definition:
RECID INTEGER ACCOUNT_NUMBER CHAR(14) AMOUNT INTEGER DESCRIPTION VARCHAR(50) THE_DATE DATE
with RECID being the primary key.
public static void main(String args[]) throws java.io.IOException, DSEObjectNotFoundException { Context tableContext; HashtableFormat tableFormat= null; // Initializing toolkit for Java if(!InitManager.isInitialized()){ InitManager.reset("file:///c:\\btt\\btt.xml"); } // Working with the table service TableService table = null; try { TableContext=ContextFactory.createContext("myTableOperationContext"); System.out.println(">>> Creating a Table Service Instance..."); table = (TableService)Service.readObject("myTableName"); // Because of the service definition, no connection to the database // is explicitly done int nbrOfRecords = 3; Trace.trace(Trace.Information, ">>> Filling the Table with " + nbrOfRecords + " records..."); for (int i = 1; i <= nbrOfRecords ; i++) { tableContext.setValueAt("ACCOUNT_NUMBER", "0007000" + i); tableContext.setValueAt("AMOUNT",new Integer(200000 + i)); tableContext.setValueAt("THE_DATE", new java.sql.Date(98,4,16)); tableContext.setValueAt("DESCRIPTION", "Adding record " + i + " in table..."); tableContext.setValueAt("RECID",new Integer(i)); tableFormat= (HashtableFormat)FormatElement.readObject("tableFormatName"); // Call the format method with argument the operation context Hashtable dataTable1 = new Hashtable(); dataTable1 = (Hashtable) tableFormat.format(tableContext); Trace.trace(Trace.Information, ">>> Adding a record to the database table..."); table.addRecord(dataTable1); } // End for // Display Table content Vector aDataVector = null; Hashtable aDataHashtable = new Hashtable(); aDataVector= table.retrieveRecordsMatching("THE_DATE=DATE('1998-05-16')"); Enumeration aDataVectorEnum = aDataVector.elements(); while (aDataVectorEnum.hasMoreElements()) { tableFormat.unformat((Hashtable)aDataVectorEnum.nextElement(), tableContext); System.out.println("Record retrieved with identification:" + tableContext.getValueAt("RECID")); } // End while // Delete record number 1 table.deleteRecordsMatching("RECID=1"); // Retrieving data of 2st record; aDataVector = table.retrieveRecordsMatching("RECID=2"); if ( !aDataVector.isEmpty()) { aDataVectorEnum = aDataVector.elements(); TableFormat.unformat((Hashtable)aDataVectorEnum.nextElement(), tableContext); System.out.println("Record retrieved with identification:" + tableContext.getValueAt("RECID")); } else { System.out.println("No records found for this search criteria"); } tableContext.setValueAt("DESCRIPTION", "Updated description"); // Updating last Record in the database table.updateRecordsMatching("RECID=(SELECT MAX(RECID) FROM"+table.getTableName()+")",tableContext,"tableFormatName"); } // End try catch (Exception e ) { System.out.println(e.getMessage()); try { table.disconnect(); } catch (Exception ex){ System.out.println(ex.getMessage());} return; } }
The Database Table Mapping service throws the following exceptions:
DSEInvalidRequestException
The current state of the object is not valid for the method being called.
Action: Check for the connection to the database and, if it is not available, create the connection before requesting any database operation.
DSEInternalErrorException
Internal data is inconsistent.
Action: Report the error to support through the standard reporting channels.
DSEInvalidArgumentException
One of the arguments in the called method is invalid. It is an instance of an unexpected class or it is outside the expected range.
Action: Check the value of the arguments.
DSEObjectNotFoundException
An object that was expected to exist during the processing of the method has not been found.
Action: Check for the correct setup of your environment.
DSESQLException
An exception has been returned when accessing the database.
Action: Refer to the SQL documentation to take the appropriate action for the specific error number and message.
DSEException
An exception has been returned when requesting or releasing a connection from a pool of connections or when trying to share a connection.
Action: Verify that the connection pooling is available. Check for the maximum number of connections defined and the connection timeout, and possibly set new values that will prevent this exception from occurring in the future.
The Database Table Mapping service has the following data externalized in the services file:
JDBCTable tag attributes
id
Name of the JDBCTable name attribute.
autoConnect
Specifies whether an automatic connection is to be made to the database. Possible values are:
true
false (default)
autoCommit
Specifies whether an automatic commit is to be performed after each statement execution. Possible values are:
true
false (default)
catalog
The name of the table catalog in the database (if it applies to the DBMS being used).
schema
The name of the table schema in the database (if it applies to the DBMS being used). It is case-sensitive based in the DBMS specifications.
table
The name of the table in the database. It is case-sensitive based in the DBMS specifications.
databaseURL
The database URL where the table is created.
user
The userid to log on to the database, if required.
password
The password to log on to the database, if required.
JDBCDriver
Name of the JDBC Driver the service will use, either to request the connection to the database or to execute the SQL statements.
poolName
Name of the connection manager pool containing the connection type you want. Consult the WebSphere administrator for this name.
dataSourceName
The DataSource object name to be used by all requests to get a connection. This should be specified when working with the connection pooling provided by WebSphere Application Server or with any other JDBC implementation of the connection pooling. This name requires a context part and a logical name part, since it will be used to do a lookup in the naming context. A typical string might look something like "jdbc/sample," where the context is "jdbc" and the logical name is "sample." This information can be supplied by the WebSphere Application Server administrator, and it identifies the DataSource object placed in the naming service.
sharedConnection
The alias of the connection the service wants to share with other JDBC services service instances.
primaryKeys
A boolean value.
If set to true (the default), the service will control the primary keys access when an update statement is executed. (By getting information about the primary keys columns defined for the database table, it will prevent the application from updating these columns if, by error, it tries to.)
If set to false, no primary keys checking is done by the service. This attribute must be set to false if the getPrimaryKeys() method is not supported by the specific JDBC implementation (as for DB2 UDB for OS/390).
statementPoolSize
Defines the dimension of the Statements pool. The default value is 32. Increase the value of the attribute to improve system performance or reduce the value to prevent exhausting system resources.
Column tag attributes
id
Name of column
dataName
Name of data field or name of the key that keeps the data field value
The tag column is used to map a specific data field in the context to a column in the database table, if the data field and the column do not have the same name. The dataName tag attribute can directly hold the data field name or a key name if a KeyedObject formatter is being used inside the Hashtable formatter definition.