Documentation Home
MySQL Connector/C++ 1.1 Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 285.1Kb
PDF (A4) - 286.0Kb

MySQL Connector/C++ 1.1 Developer Guide  /  ...  /  Using a PreparedStatement for a Stored Procedure That Returns No Result

7.3.1 Using a PreparedStatement for a Stored Procedure That Returns No Result

This example shows how to call a stored procedure that returns no result set.

  1. Make a copy of the tutorial framework code:

    $> cp framework.cpp ps_scenario1.cpp
  2. Add the following code to the try block of the tutorial framework:

    vector<string> code_vector;
    vector<string> name_vector;
    vector<string> cont_vector;
    cont_vector.push_back("North America");
    sql::Driver * driver = get_driver_instance();
    std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
    std::auto_ptr< sql::PreparedStatement >  pstmt;
    pstmt.reset(con->prepareStatement("CALL add_country(?,?,?)"));
    for (int i=0; i<3; i++)

    Also, uncomment #include <vector> near the top of the code, because vectors are used to store sample data.

  3. Compile the program as described in Section 7.1, “Prerequisites and Background Information”.

  4. Run the program:

    $> ./ps_scenario1
  5. You can check whether the database has been updated correctly by using this query:

    mysql> SELECT Code, Name, Continent FROM Country
        -> WHERE Code IN('DSN','ATL','SLD');
    | Code | Name       | Continent     |
    | ATL  | Atlantis   | Oceania       |
    | DSN  | Disneyland | North America |
    | SLD  | Sealand    | Europe        |

The code is relatively simple, as no processing is required to handle result sets. The procedure call, CALL add_country(?,?,?), is made using placeholders for input parameters denoted by '?'. These placeholders are replaced by the appropriate data values using the PreparedStatement object's setString method. The for loop is set up to iterate 3 times, as there are three data sets in this example. The same PreparedStatement is executed three times, each time with different input parameters.