Home | Previous Page | Next Page   Working With Informix Types > Named and Unnamed Rows >

Using the ClassGenerator Utility

The ClassGenerator utility generates a Java class for a named row type defined in the system catalog. The utility is an Informix extension to Sun's JDBC specification.

The created Java class implements the java.sql.SQLData interface. The class has members for each field in the named row. The readSQL(), writeSQL(), and SQLData.readSQL() methods read the attributes in the order in which they appear in the definition of the named row type in the database. Similarly, writeSQL() writes the data to the stream in that order.

ClassGenerator is packaged in the ifxtools.jar file, so the CLASSPATH environment variable must point to ifxtools.jar.

The syntax for using ClassGenerator is as follows:

java ClassGenerator rowtypename [-u URL] [-c classname]

The default value for classname is the value for rowtypename.

If the URL parameter is not specified, the required information is retrieved from the setup.std file in the home directory.

The structure of setup.std is as follows:

URL jdbc:host-name:port-number
informixserver informixservername
database database
user user
passwd password

Simple Named Row Example

To use ClassGenerator, you first create the named row on the database server as shown in this example:

create row type employee (name char (20), age int);

Next, run ClassGenerator:

java ClassGenerator employee 

The class generator generates employee.java, as shown next, and retrieves the database URL information from setup.std, which has the following contents:

URL jdbc:davinci:1528
database test
user scott
passwd tiger
informixserver picasso_ius

Following is the generated .java file:

import java.sql.*;
import java.math.*;
public class employee implements SQLData
{
   public String name;
   public int age;
   private String sql_type;

   public String getSQLTypeName() { return "employee"; }

   public void readSQL (SQLInput stream, String type) throws 
      SQLException
   {
      sql_type = type;
      name = stream.readString();
      age = stream.readInt();
   }

   public void writeSQL (SQLOutput stream) throws SQLException
   {
      stream.writeString(name);
      stream.writeInt(age);
   }
}

Nested Named Row Example

To use ClassGenerator for a nested row, you first create the named row on the database server:

create row type manager (emp employee, salary int);

Next, run ClassGenerator. In this case, the setup.std file is not consulted, because you provide all the needed information at the command line:

java ClassGenerator manager -c Manager -u "jdbc:davinci:1528/test:user=scott;
password=tiger;informixserver=picasso_ius"

The -c option defines the Java class you are creating, which is Manager (with uppercase M).

The preceding command generates the following Java class:

import java.sql.*;
import java.math.*;
public class Manager implements SQLData
{
   public employee emp;
   public int salary;
   private String sql_type;

   public String getSQLTypeName() { return "manager"; }

   public void readSQL (SQLInput stream, String type) throws 
      SQLException
   {
      sql_type = type;
      emp = (employee)stream.readObject();
      salary = stream.readInt();
   }

   public void writeSQL (SQLOutput stream) throws SQLException
   {
      stream.writeObject(emp);
      stream.writeInt(salary);
   }
}
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]