Navigation:  Advantage Developer's Guide > Part III - Accessing Advantage Data > Chapter 16 - Advantage and Java >

     Calling a Stored Procedure

Advantage Database Server v8.1: A Developer’s Guide

by Cary Jensen and Loy Anderson

  © 2007 Cary Jensen and Loy Anderson. All rights reserved.

Previous pageReturn to chapter overviewNext page

Calling a stored procedure is no different than executing any other query. If your stored procedure does not require input parameters, you use a Statement instance. You use a PreparedStatement instance if there are one or more input parameters. If the stored procedure returns one or more records, you invoke the executeQuery method of the Statement or PreparedStatement object, and you invoke the execute or the executeUpdate methods when the stored procedure does not return records.

Invoking a stored procedure that takes one input parameter is demonstrated by the following code associated with the actionPerformed event handler for the Show 10% of Invoices button (shown in Figure 16-1). The stored procedure referenced in this code is the SQL stored procedure created in Chapter 7. If you did not create this stored procedure, but created one of the other stored procedures described in that chapter, substitute the name of the stored procedure object in your data dictionary in the EXECUTE PROCEDURE string, like this:

void callStoredProcBtn_actionPerformed(ActionEvent e) {
 PreparedStatement getCustStmt;
 if (custNoText.getText() == "") {
   System.out.println("Enter a customer ID");
   return;
 }
 try {
   getCustStmt = conn.prepareStatement(
     "EXECUTE PROCEDURE SQLGet10Percent( ? )" );
   getCustStmt.setInt( 1,
     Integer.parseInt(paramText.getText()));
   ResultSet rs = getCustStmt.executeQuery();
   if (isRSEmpty(rs)) {
     jTable1.setModel(new ResultTableModel(null));
     JOptionPane.showMessageDialog(this,
      "No records in result set");
     return;
   }
   jTable1.setModel(new ResultTableModel(rs));
 }
 catch (Exception e1) {
   JOptionPane.showMessageDialog(this,
     e1.getMessage());
 }
}