This example shows how to handle result sets produced by a stored procedure.
-
Make a copy of the tutorial framework code:
$> cp framework.cpp ps_scenario3.cpp
-
Add the following code to the
try
block of the tutorial framework:sql::Driver * driver = get_driver_instance(); std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass)); con->setSchema(database); std::auto_ptr< sql::PreparedStatement > pstmt; std::auto_ptr< sql::ResultSet > res; pstmt.reset(con->prepareStatement("CALL get_data()")); res.reset(pstmt->executeQuery()); for(;;) { while (res->next()) { cout << "Name: " << res->getString("Name") << " Population: " << res->getInt("Population") << endl; } if (pstmt->getMoreResults()) { res.reset(pstmt->getResultSet()); continue; } break; }
Compile the program as described in Section 7.1, “Prerequisites and Background Information”.
-
Run the program:
$> ./ps_scenario3
Make a note of the output generated.
The code executes the stored procedure using a
PreparedStatement
object. The standard
do
/while
construct is used
to ensure that all result sets are fetched. The returned values
are fetched from the result sets using the
getInt
and getString
methods.