Powered by SmartDoc

The jdbc.ee option

A jdbc.ee option enables Relaxer tables object to use J2EE functions to access RDBMS tables.

Parameters

The jdbc.ee option takes one of the following values as a parameter:

true
Uses J2EE functions to access RDBMS tables.
false
Does not use J2EE functions to access RDBMS tables.

The default configuration is false. No parameter implies that the parameter is true.

Artifact

The jdbc.ee option generates no additional classes. The jdbc.ee option just modified the behavior of Relaxer Table Objects.

The jdbc.ee option adds no additional methods to Relaxer objects.

Example

List 12.3.3.1[jdbcEe.rng] is a sample schema for the jdbc.ee option.

jdbcEe.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner">
        <data type="token"/>
      </element>
      <ref name="address"/>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

Build

Execution of Relaxer with the jdbc.ee option is as follows. The jdbc option must be used together.

$ relaxer -jdbc -jdbc.ee jdbc.rng

As a result, Relaxer generates 13 files:

There are 12 Java classes and one DDL file.

Account, Address, and Phone are Relaxer objects to be mapped the RELAX schema jdbc.rng.

URelaxer, RStack, and UJAXP are helper objects for Relaxer objects to execute.

These six objects are generated as simply specifing the java option.

The account.ddl, shown as List 12.3.3.1.1[account.ddl], is a DDL for a RDBMS table.

account.ddl
CREATE TABLE "account" (
	"accountNo" VARCHAR(32) NOT NULL,
	"balance" INTEGER NOT NULL,
	"owner" VARCHAR(32) NOT NULL,
	"address" VARCHAR(128) NOT NULL,
	"address_zip" VARCHAR(32) NOT NULL,
	"phone" VARCHAR(512)
)

Six ohter objects are JDBC-specific objects.

AccountTable is a Relaxer table object. AccountTableView is a Relaxer table view object. IAccountTableView is a Relaxer table view interface. AccountList is a Relaxer table list object.

USQL and RJDBCList are helper object that used by Relaxer table objects.

Insert Record

List 12.3.3.2.1[JdbcEeInsert.java] is a sample program for the jdbcEe.rng.

JdbcEeInsert.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;
import javax.naming.NamingException;

public class JdbcEeInsert {
    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException,
	       ClassNotFoundException, SQLException, NamingException {

	String jdbcUrlOrJndiName = args[0];
	String tableName = args[1];
	String driverName = args[2];
	String uri = args[3];
	enableDriver(driverName);
	Account account = makeAccount(uri);
	insertRecord(jdbcUrlOrJndiName, tableName, account);
    }

    private static Account makeAccount(String uri)
	throws IOException, SAXException, ParserConfigurationException {

	System.out.println("*** makeAccount ***");
	Account account = new Account(uri);
	System.out.println(account);
	return (account);
    }

    private static void enableDriver(String driverName)
	throws ClassNotFoundException {

	System.out.println("*** enableDriver ***");
	Class.forName(driverName);
    }

    private static void insertRecord(
	String jdbcUrlOrJndiName,
	String tableName,
	Account account
    ) throws SQLException, NamingException {
	System.out.println("*** insertRecord ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrlOrJndiName, tableName);
	    table.insert(account);
	} finally {
	    if (table != null) {
		table.close();
	    }
	}
    }
}

Compilation of the JdbcEeInsert is shown here:

$ javac JdbcEeInsert.java

An application can insert a record, either a Java object or an XML document, using the insert method of the Relaxer object.

Select Record

List 12.3.3.3.1[JdbcEeSelect.java] is a sample program for the jdbcEe.rng.

JdbcEeSelect.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;
import javax.naming.NamingException;

public class JdbcEeSelect {
    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException,
	       ClassNotFoundException, SQLException, NamingException {

	String jdbcUrlOrJndiName = args[0];
	String tableName = args[1];
	String driverName = args[2];
	enableDriver(driverName);
	Account[] accounts = selectRecord(jdbcUrlOrJndiName, tableName);
	printAccounts(accounts);
    }

    private static void enableDriver(String driverName)
	throws ClassNotFoundException {

	System.out.println("*** enableDriver ***");
	Class.forName(driverName);
    }

    private static Account[] selectRecord(
	String jdbcUrlOrJndiName,
	String tableName
    ) throws SQLException, NamingException {
	System.out.println("*** selectRecord ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrlOrJndiName, tableName);
	    Account[] accounts = table.select();
	    return (accounts);
	} finally {
	    if (table != null) {
		table.close();
	    }
	}
    }

    private static void printAccounts(Account[] accounts) {
	System.out.println("*** printAccounts ***");
	for (int i = 0;i < accounts.length;i++) {
	    System.out.println(accounts[i]);
	}
    }
}

Compilation of the JdbcEeSelect is shown here:

$ javac JdbcEeSelect.java

An application can select all records from the table using the select method of the Relaxer object.

Execution - Insert

List 12.3.3.4.1[jdbcEe.xml] is a valid XML document against the jdbc.rng.

jdbcEe.xml
<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JdbcEeInsert for the valid document is shown here:

$ java -classpath ".;vdb.jar" JdbcEeInsert \
    java:comp/env/jdbc/account account org.vdb.Driver jdbcEe.xml

As a result, the XML document jdbcEe.xml is inserted to the RDBMS table account as a database record.

Execution - Select

Execution of the JdbcEeSelect for the valid document is shown here:

$ java -classpath ".;vdb.jar" JdbcEeSelect \
    java:comp/env/jdbc/account account org.vdb.Driver
<account accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

As a result, all records the RDBMS table account contains dumps as XML documents by "jdbc:vdb:accountDb".