3.6. Writing the Stored Procedure For Retrieving Records

The second stored procedure retrieves the words for "hello" and "world" based on the language specified. This procedure starts very much like Insert.java. So start the text editor to create a new Java class file called Select.java and add the code to import the VoltDB libraries and begin a class named Select. Note that the code is almost identical, except for the class name.

import org.voltdb.*;

public class Select extends VoltProcedure {

Next, we need to write the SQL statement that fetches the necessary data. In this case we want to find a record based on the field DIALECT and return the values for HELLO and WORLD. So the statement looks like the following. Note that we again use a question mark for the specific value of DIALECT, since that will be passed as a parameter to the stored procedure. We also provide the appropriate arguments when we add the SQL statement to the queue.

  public final SQLStmt sql = new SQLStmt(
      "SELECT HELLO, WORLD FROM HELLOWORLD " +
      " WHERE DIALECT = ?;"
  );

  public VoltTable[] run( String language) 
      throws VoltAbortException {
          voltQueueSQL( sql, language );
          return voltExecuteSQL();
      }
}

The major difference between the insert and the select procedures is that in select we must find a way to return the values fetched from the database. VoltDB provides a utility to help you do this called the VoltTable. VoltTable is an array used as the return value of all procedure calls. The Select procedure returns the VoltTable array containing the rows returned by the Select statement(s) directly to the calling client application.

Your procedure is complete. Save the file and close the text editor. The completed procedure is shown in Example A.3, “Select.java”.