Skip to main content

This version of the product is no longer supported, and this documentation is no longer updated regularly. See the latest version of this content.Opens in a new tab

Executing a Stored Procedure

You can also execute stored procedures on Caché. The following method executes the RetrieveByContactType procedure stored in Contact. This procedure accepts a single parameter representing the value of the ContactType property. It returns the value of Name for all Contact instances that have a value for ContactType matching the input parameter. The method iterates through the ResultSet returned by the procedure and displays its values.


public class JDBCExamples {
   public static void printContactByType(String type)
   throws SQLException, ClassNotFoundException{
      Connection conn = JDBCExamples.createConnection();
      CallableStatement cs = conn.prepareCall
      ("{call JavaTutorial.Contact_RetrieveByContactType(?)}");
      cs.setString(1,type);
      ResultSet rs = cs.executeQuery();
      while (rs.next()){
         System.out.println(rs.getString(1));
      }
      rs.close();
   }
}

The method uses the java.sql.Connection prepareCall to create the java.sql.CallableStatement object needed to execute the stored procedure. Again, com.intersys.objects.Database provides a prepareCall method that can also be used to create the java.sql.CallableStatement object.

Note:

Stored procedures can be defined as Class Queries within Caché class definitions. To learn more about stored procedures and class queries read Class Queries in Using Caché Objects and Defining and Using Stored Procedures in Using Caché SQL.

FeedbackOpens in a new tab