Skip to main content

Executing a Query

Once you have a connection to Caché, represented either by java.sql.Connection or com.intersys.objects.Database, you can execute SQL statements against the database. The following method executes an SQL SELECT operation. The query retrieves the Name and ContactType values for all Contact instances in the database. The method iterates through the ResultSet displaying each value for Name and ContactType. This method uses java.sql.Connection to create the Statement object used to execute the query.


public class JDBCExamples {
   public static void printContactNames()
   throws SQLException, ClassNotFoundException{
      Connection conn = JDBCExamples.createConnection();
      Statement stmt = conn.createStatement();
      String query =
         "SELECT Name, ContactType FROM JavaTutorial.Contact";
      ResultSet rs=stmt.executeQuery(query);
      while (rs.next()){
         String name = rs.getString(1);
         String type = rs.getString(2);
         System.out.println("Name: " + name + " Type: " + type);
      }
      rs.close();
   }
}
FeedbackOpens in a new tab