Receiving Java objects from the database

A client JDBC application can receive a Java object from the database in a result set or as the value of an output parameter returned from a stored procedure. If a result set contains a Java object as column data, use one of the following getObject methods in the ResultSet interface to retrieve the object:

Object getObject(int columnIndex) throws SQLException;
Object getObject(String columnName) throws SQLException;

If an output parameter from a stored procedure contains a Java object, use the following getObject method in the CallableStatement interface to retrieve the object:

Object getObject(int parameterIndex) throws SQLException;

The following example illustrates the use of ResultSet.getObject(int parameterIndex) to assign an object received in a result set to a class variable. The example uses the Address class and Friends table used in the previous section and presents a simple application that prints a name and address on an envelope.

/*
 ** This application takes a first and last name, gets the 
 ** specified person’s address from the Friends table in the 
 ** database, and addresses an envelope using the name and
 ** retrieved address.
 */
 public class Envelope
 {
   Connection conn = null;
   String firstName = null;
   String lastName = null;
   String street = null;
   String city = null;
   String zip = null;
 
   public static void main(String[] args)
   {
     if (args.length < 2)
     {
     System.out.println("Usage: Envelope <firstName> 
       <lastName>");
     System.exit(1);
     }
     // create a 4" x 10" envelope
     Envelope e = new Envelope(4, 10);
     try
     {
       // connect to the database with the Friends table.
       conn = DriverManager.getConnection(
         "jdbc:sybase:Tds:localhost:5000", "username", 
           "password");
       // look up the address of the specified person
       firstName = args[0];
       lastName = args[1];
       PreparedStatement ps = conn.prepareStatement(
         "SELECT address FROM friends WHERE " +
           "firstname = ? AND lastname = ?");
       ps.setString(1, firstName);
       ps.setString(2, lastName);
       ResultSet rs = ps.executeQuery();
       if (rs.next())
       {
         Address a = (Address) rs.getObject(1);
         // set the destination address on the envelope
         e.setAddress(firstName, lastName, a);
       }
       conn.close();
     }
     catch (SQLException sqe)
     {
       sqe.printStackTrace();
       System.exit(2);
     }
     // if everything was successful, print the envelope
     e.print();
   }
   private void setAddress(String fname, String lname, Address a)
   {
     street = a.streetNumber + " " + a.street + " " +
       a.apartmentNumber;
     city = a.city;
     zip = "" + a.zipCode;
   }
   private void print()
   {
     // Print the name and address on the envelope.
     ...
   }
 }

You can find a more detailed example of HandleObject.java in the sample2 subdirectory under your jConnect installation directory.