Skip to main content

Executing a Dynamic Query: CacheQuery

A Java client can also execute a dynamic query against Caché by passing a SQL string to the database through the CacheQuery class. The CacheQuery execute method returns a java.sql.ResultSet object.

The following method executes a query that selects the value of the Number property for all PhoneNumber instances whose Contact property has the value id and whose PhoneNumberType property has the value type. The method then iterates through the ResultSet and displays each of the Number values.


public class BindingExamples {
   public static void displayPhoneNumbersByType(Database db,
   String id, String type) throws CacheException, SQLException {
      Id ID= new Id(id);
      String SQL = "SELECT Number FROM JavaTutorial.PhoneNumber" +
      " WHERE Contact='"+id +"' AND PhoneNumberType='" + type +"'";
      CacheQuery query = new CacheQuery(db, SQL);
      java.sql.ResultSet rs= query.execute();
      while (rs.next()){
         System.out.println(rs.getString(1));
      }
   }
}
Note:

The SQL table representing PhoneNumber in Caché contains a Contact column. This column contains Contact object id values. These values represent foreign keys associating each PhoneNumber row with a Contact row. This is discussed in part II of this tutorial.

FeedbackOpens in a new tab