Powered by SmartDoc

The jdbc option

A jdbc option is a generator option. This option enables Relaxer to generate Java clasess to access RDBMS tables via JDBC.

Parameters

The jdbc option take no parameter. Specifing the jdbc option means that you want to use the JDBC Generator.

Artifact

The jdbc option generates additional classes as follows:

RecordTable is a Relaxer table object. RecordTableView is a Relaxer table view object. IRecordTableView is a Relaxer table view interface. RecordList is a Relaxer table list object.

USQL and RJDBCList are helper objects to be used by Relaxer table objects.

In addition these classes, one file, record.ddl is generated.

The jdbc option adds no additional methods to Relaxer objects.

Example

List 12.2.3.1[jdbc.rng] is a sample schema for the jdbc option.

jdbc.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 option is as follows:

$ relaxer -jdbc 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 by simply specifing the java option.

The account.ddl, shown as List 12.2.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)
)

The ohter six 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 objects to be used by Relaxer table objects.

Create Table

List 12.2.3.2.1[JdbcCreate.java] is a sample program to create a table from the jdbc.rng.

JdbcCreate.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;

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

	String jdbcUrl = args[0];
	String tableName = args[1];
	String driverName = args[2];
	enableDriver(driverName);
	createTable(jdbcUrl, tableName);
    }

    private static void enableDriver(String driverName)
	throws ClassNotFoundException {

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

    private static void createTable(String jdbcUrl, String tableName)
	throws SQLException {

	System.out.println("*** createTable ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrl, tableName);
	    table.createTable();
	} finally {
	    if (table != null) {
		table.close();
	    }
	}
    }
}

Compilation of the JdbcCreate is shown here: No special options are required.

$ javac JdbcCreate.java

An application can create a table that use the createTable method of the Relaxer object.

Insert Record

List 12.2.3.3.1[JdbcInsert.java] is a sample program that inserts records into the table generated of the jdbc.rng.

JdbcInsert.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;

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

	String jdbcUrl = args[0];
	String tableName = args[1];
	String driverName = args[2];
	String uri = args[3];
	enableDriver(driverName);
	Account account = makeAccount(uri);
	insertRecord(jdbcUrl, 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 jdbcUrl,
	String tableName,
	Account account
    ) throws SQLException {
	System.out.println("*** insertRecord ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrl, tableName);
	    table.insert(account);
	} finally {
	    if (table != null) {
		table.close();
	    }
	}
    }
}

Compilation of the JdbcInsert is shown here: No special options are required.

$ javac JdbcInsert.java

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

Select Record

List 12.2.3.4.1[JdbcSelect.java] is a sample program that selects all records from the table generated by the jdbc.rng.

JdbcSelect.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;

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

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

    private static void enableDriver(String driverName)
	throws ClassNotFoundException {

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

    private static Account[] selectRecord(
	String jdbcUrl,
	String tableName
    ) throws SQLException {
	System.out.println("*** selectRecord ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrl, 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 JdbcSelect is shown here: No special options are required.

$ javac JdbcSelect.java

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

Drop Table

List 12.2.3.5.1[JdbcDrop.java] is a sample program to drop the table of the jdbc.rng.

JdbcDrop.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.sql.SQLException;

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

	String jdbcUrl = args[0];
	String tableName = args[1];
	String driverName = args[2];
	enableDriver(driverName);
	dropTable(jdbcUrl, tableName);
    }

    private static void enableDriver(String driverName)
	throws ClassNotFoundException {

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

    private static void dropTable(String jdbcUrl, String tableName)
	throws SQLException {

	System.out.println("*** dropTable ***");
	AccountTable table = null;
	try {
	    table = new AccountTable(jdbcUrl, tableName);
	    table.dropTable();
	} finally {
	    if (table != null) {
		table.close();
	    }
	}
    }
}

Compilation of the JdbcDrop is shown here: No special options are required.

$ javac JdbcDrop.java

An application can drop the table using the dropTable method of the Relaxer object.

Execution - Create

Execution of JdbcCreate for a valid document is shown here:

$ java -classpath ".;vdb.jar" JdbcCreate jdbc:vdb:accountDb \
    account org.vdb.Driver

Execution - Insert

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

jdbc.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 JdbcInsert for valid documents is shown here:

$ java -classpath ".;vdb.jar" JdbcInsert jdbc:vdb:accountDb \
    account org.vdb.Driver jdbc.xml

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

Execution - Select

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

$ java -classpath ".;vdb.jar" JdbcSelect jdbc:vdb:accountDb \
    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 that the RDBMS table account contains are dumped as XML documents by "jdbc:vdb:accountDb".

Execution - Drop

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

$ java -classpath ".;vdb.jar" JdbcDrop jdbc:vdb:accountDb account \
    org.vdb.Driver

As a result, the RDBMS table account is dropped in the RDMBS (identified by "jdbc:vdb:accountDb").