Accessing a database table

About this task
As a subclass of the JDBCService class, the JDBCTable class has an external definition that allows any attribute of the JDBCTable object to be defined in the services definition file. All attributes of the JDBCTable class are defined externally, including those inherited from the JDBCService class, which in turn inherits from the Service class. A JDBCTable object is usually instantiated by providing the name of the service to the ServiceExternalizer. The ServiceExternalizer creates an instance of the class, identified by the name (in this case, a JDBCTable), and sets its attributes to the values read from the toolkit services definition file.

After instantiation of a JDBCTable service, a name and a tableName (which includes the schema name and the catalog name, if necessary) are assigned to the JDBCTable instance. The datasource attribute, and the connManager attribute are also set. Before an application can work with the JDBCTable service, a connection to the database must be created. The connection is stored in the JDBCTable attribute databaseConnection. When access to the database table is requested, the service checks if a connection to the database already exists. If no connection has been previously established, an exception is thrown.

If no automatic connection to the database is requested from the JDBCTable service, then the connection can be created in either of two ways:
  • By calling one of the JDBCTable instance connect methods, as described in the Database Services documentation. This is the recommended way. The JDBCTable will lookup the datasourceName to get a Connection.
  • By using the standard JDBC DriverManager interface to create the connection, and setting the databaseConnection attribute to the returned Connection instance. This is not recommending for DriverManager will use low-level JDBC access.

Note that this service is working with existing database tables, so the application must set the primary key column value for any record to be inserted in the table, define this column as part of the JDBCTable service format definition, and add the name mapping between the data field that will contain this primary key and the table column into the service definition if this is needed.