MySQL :: Developing Database Applications Using MySQL Connector/C

来源:百度文库 编辑:神马文学网 时间:2024/04/29 22:50:32
Skip navigation linksThe world's most popular open source database

Login | Register


  • Developer Zone
  • Downloads
  • Documentation
  • Articles
   
  • Articles

About the Author

Giri Mandalika is a software engineer in Sun's ISV Engineeringorganization. Giri works with partners and ISVs to make Sun thepreferred vendor of choice for deploying enterprise applications.Currently, Giri is focused on standard benchmarks, optimization, andscalability of enterprise applications on Sun platforms.



We are always looking for interesting articlesabout MySQL! Have you written something and would like to it publishedhere? Please contact us via feedback form »

Related Pages:

  • Installing MySQL Connector/C++ from Source

Developing Database Applications Using MySQL Connector/C++

This tutorial will show you the essential steps to build and installMySQL Connector/C++ driver, with simple examples to connect, insert,and retrieve data from a MySQL database. Because the focus is ondatabase connectivity from a C++ application, this document assumesthat some kind of MySQL database is already up and accessible from theclient machine.

Application developers who are new to MySQL Connector/C++ but not toC++ programming and MySQL database, are the target audience of thistutorial.

Listed below are the tools and technologies used to compile, build and run the examples in this tutorial.

Database MySQL Server 5.1.24-rc C++ Driver MySQL Connector/C++ 1.0.5 MySQL Client Library MySQL Connector/C 6.0 Compiler Sun Studio 12 C++ compiler Make CMake 2.6.3 Operating System OpenSolaris 2008.11 32-bit CPU / ISA Intel Centrino / x86 Hardware Toshiba Tecra M2 Laptop

CONTENTS

  • MySQL C++ Driver Based on JDBC 4.0 Specification
  • Installing MySQL Connector/C++
  • Runtime Dependencies
  • IDE for Developing C++ Applications
  • Create the City Table in the test Database for Code Examples
  • Testing the MySQL Database Connectivity With the Connector/C++
  • Using Prepared Statements
  • Using Transactions
  • Accessing Result Set Metadata
  • Accessing Database Metadata
  • Accessing Parameter Metadata from a PreparedStatement Object
  • Catching Exceptions
  • Debug Tracing with MySQL Connector/C++
  • For More Information

MySQL C++ Driver Based on JDBC 4.0 Specification

MySQL Connector/C++ is one of the latest connectors for MySQL,developed by Sun Microsystems. The MySQL connector for C++ provides anobject-oriented application programming interface (API) and a databasedriver for connecting C++ applications to the MySQL Server.

The development of Connector/C++ took a different approach comparedto the existing drivers for C++ by implementing the JDBC API in C++world. In other words, Connector/C++ driver's interface is mostly basedon Java programming language's JDBC API. Java Database Connectivity(JDBC) API is the industry standard for connectivity between the Javaprogramming language and a wide range of databases. Connector/C++implemented a significant percentage of the JDBC 4.0 specification. C++application developers who are familiar with JDBC programming may findthis useful, and as a result, it could improve application developmenttime.

The following classes were implemented by the MySQL Connector/C++.

  • Driver
  • Connection
  • Statement
  • PreparedStatement
  • ResultSet
  • Savepoint
  • DatabaseMetaData
  • ResultSetMetaData
  • ParameterMetaData

The Connector/C++ driver can be used to connect to MySQL 5.1 and later versions.

Prior to MySQL Connector/C++, C++ application developers were required to use either the non-standard & procedural MySQL C API directly or the MySQL++ API, which is a C++ wrapper for the MySQL C API.

Installing MySQL Connector/C++

Binary Installation

Starting with release 1.0.4, Connector/C++ is available in binaryform for Solaris, Linux, Windows, FreeBSD, Mac OS X, HP-UX and AIXplatforms. MSI installer and a binary zip file without the installer isavailable for Windows, where as the binary package is available ascompressed GNU TAR archive (tar.gz) for the rest of the platforms. Youcan download the pre-compiled binary from the Connector/C++ download page.

Installation from the binary package is very straight forward onWindows and other platforms - simply unpacking the archive in a desiredlocation installs the Connector/C++ driver. Both statically linked andthe dynamically linked Connector/C++ driver can be found in libdirectory under the driver installation directory. If you plan to usethe dynamically linked version of MySQL Connector/C++, ensure that theruntime linker can find the MySQL Client Library. Consult youroperating system documentation for the steps to modify and expand thesearch path for libraries. In case you cannot modify the library searchpath, copy your application, the MySQL Connector/C++ driver and theMySQL Client Library into the same directory. This approach may work onmost of the platforms as they search the originating directory bydefault, before searching for the required dynamic libraries elsewhere.

Source Installation

Those who want to build the connector driver from the source code, please check the Installing MySQL Connector/C++ from Source page for detailed instructions.

Runtime Dependencies

Because the Connector/C++ driver is linked against the MySQL ClientLibrary, dynamically linked C++ applications that use Connector/C++will require the MySQL client programming support installed along withthe connector driver, on the machines where the application is supposedto run. Static linking with the MySQL Client Library is one of theoptions to eliminate this runtime dependency. However due to various reasons, static linking is discouraged in building larger applications.

IDE for Developing C++ Applications

If you are looking for an integrated development environment (IDE)to develop C/C++ applications, consider using the free and open NetBeans platform. NetBeans C/C++ Development Packlets C/C++ developers use their specified set of compilers and tools inconjunction with NetBeans IDE to build native applications on Solaris,Linux, Windows and Mac OS X. The C/C++ development pack makes theeditor language-aware for C/C++ and provides project templates, adynamic class browser, Makefile support &m debugger functionality.It is possible to extend C/C++ development pack base functionality withnew features, modules and plug-ins.

MySQL Connector/C++: How to Build a Client using NetBeans 6.5 (for Dummies)tutorial has instructions to use NetBeans IDE to build clientapplications based on Connector/C++. In addition to the above tutorial,Installing and Configuring C/C++ Supporttutorial on NetBeans.org web site will help you with the installationand configuration steps for the NetBeans C/C++ development pack, and Getting Started With the NetBeans C/C++ Development Pack tutorial provides the basic steps involved in developing a C/C++ application using the NetBeans C/C++ Development Pack.

Create the City Table in the test Database for Code Examples

The code samples in this tutorial try to retrieve the data from the City table in the MySQL test database. The table structure and the data from the City table are shown here by using the mysql client. The MySQL Server is running on the default port 3306.

# mysql -u root -pEnter password: adminWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.1.24-rc-standard Source distributionType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> USE test;Database changedmysql> DESCRIBE City;+----------+-------------+------+-----+---------+-------+| Field    | Type        | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| CityName | varchar(30) | YES  |       | NULL  |       |+----------+-------------+------+-----+---------+-------+1 row in set (0.07 sec)mysql> SHOW CREATE TABLE City\G*************************** 1. row ***************************Table: CityCreate Table: CREATE TABLE `City` (`CityName` varchar(30) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=ascii1 row in set (0.00 sec)mysql> SELECT * FROM City;+--------------------+| CityName           |+--------------------+| Hyderabad, India   || San Francisco, USA || Sydney, Australia  |+--------------------+3 rows in set (0.17 sec)

bash shell was used to show all the examples in this tutorial.

Testing the MySQL Database Connectivity With the Connector/C++

The following C++ code sample demonstrates how to connect to a MySQLServer running on the same host, using the MySQL Connector for C++. Thecode sample connects to the MySQL database test by using the JDBC like API provided by the Connector C++, executes a query to retrieve all the rows from the table City, extracts the data from the result set and displays it on the standard output, inserts couple of rows of data into the table City using the Prepared Statements, demonstrates transactions using savepoints and examines the result set and database metadata.

The sample codeis provided only for the purpose of demonstration. It does notrecommend the readers to adopt a particular style of coding. To keep itsimple, the sample codeassumes that the user always provides well-formed input - hence thereis no explicit error checking code in the following example. Usediscretion in re-using the sample code. (Download the sample code)

# cat MySQLConnectorC++Client.cpp/* Standard C++ headers */#include #include #include #include #include /* MySQL Connector/C++ specific headers */#include #include #include #include #include #include #include #include #include #define DBHOST "tcp://127.0.0.1:3306"#define USER "root"#define PASSWORD "admin"#define DATABASE "test"#define NUMOFFSET 100#define COLNAME 200using namespace std;using namespace sql;static void retrieve_data_and_print (ResultSet *rs, int type, int colidx, string colname) {/* retrieve the row count in the result set */cout << "\nRetrieved " << rs -> rowsCount() << " row(s)." << endl;cout << "\nCityName" << endl;cout << "--------" << endl;/* fetch the data : retrieve all the rows in the result set */while (rs->next()) {if (type == NUMOFFSET) {cout << rs -> getString(colidx) << endl;} else if (type == COLNAME) {cout << rs -> getString(colname) << endl;} // if-else} // whilecout << endl;} // retrieve_data_and_print()static void retrieve_dbmetadata_and_print (Connection *dbcon) {if (dbcon -> isClosed()) {throw runtime_error("DatabaseMetaData FAILURE - database connection closed");}cout << "\nDatabase Metadata" << endl;cout << "-----------------" << endl;cout << boolalpha;/* The following commented statement won't work with Connector/C++ 1.0.5 and later *///auto_ptr < DatabaseMetaData > dbcon_meta (dbcon -> getMetaData());DatabaseMetaData *dbcon_meta = dbcon -> getMetaData();cout << "Database Product Name: " << dbcon_meta -> getDatabaseProductName() << endl;cout << "Database Product Version: " << dbcon_meta -> getDatabaseProductVersion() << endl;cout << "Database User Name: " << dbcon_meta -> getUserName() << endl << endl;cout << "Driver name: " << dbcon_meta -> getDriverName() << endl;cout << "Driver version: " << dbcon_meta -> getDriverVersion() << endl << endl;cout << "Database in Read-Only Mode?: " << dbcon_meta -> isReadOnly() << endl;cout << "Supports Transactions?: " << dbcon_meta -> supportsTransactions() << endl;cout << "Supports DML Transactions only?: " << dbcon_meta -> supportsDataManipulationTransactionsOnly() << endl;cout << "Supports Batch Updates?: " << dbcon_meta -> supportsBatchUpdates() << endl;cout << "Supports Outer Joins?: " << dbcon_meta -> supportsOuterJoins() << endl;cout << "Supports Multiple Transactions?: " << dbcon_meta -> supportsMultipleTransactions() << endl;cout << "Supports Named Parameters?: " << dbcon_meta -> supportsNamedParameters() << endl;cout << "Supports Statement Pooling?: " << dbcon_meta -> supportsStatementPooling() << endl;cout << "Supports Stored Procedures?: " << dbcon_meta -> supportsStoredProcedures() << endl;cout << "Supports Union?: " << dbcon_meta -> supportsUnion() << endl << endl;cout << "Maximum Connections: " << dbcon_meta -> getMaxConnections() << endl;cout << "Maximum Columns per Table: " << dbcon_meta -> getMaxColumnsInTable() << endl;cout << "Maximum Columns per Index: " << dbcon_meta -> getMaxColumnsInIndex() << endl;cout << "Maximum Row Size per Table: " << dbcon_meta -> getMaxRowSize() << " bytes" << endl;cout << "\nDatabase schemas: " << endl;auto_ptr < ResultSet > rs ( dbcon_meta -> getSchemas());cout << "\nTotal number of schemas = " << rs -> rowsCount() << endl;cout << endl;int row = 1;while (rs -> next()) {cout << "\t" << row << ". " << rs -> getString("TABLE_SCHEM") << endl;++row;} // whilecout << endl << endl;} // retrieve_dbmetadata_and_print()static void retrieve_rsmetadata_and_print (ResultSet *rs) {if (rs -> rowsCount() == 0) {throw runtime_error("ResultSetMetaData FAILURE - no records in the result set");}cout << "ResultSet Metadata" << endl;cout << "------------------" << endl;/* The following commented statement won't work with Connector/C++ 1.0.5 and later *///auto_ptr < ResultSetMetaData > res_meta ( rs -> getMetaData() );ResultSetMetaData *res_meta = rs -> getMetaData();int numcols = res_meta -> getColumnCount();cout << "\nNumber of columns in the result set = " << numcols << endl << endl;cout.width(20);cout << "Column Name/Label";cout.width(20);cout << "Column Type";cout.width(20);cout << "Column Size" << endl;for (int i = 0; i < numcols; ++i) {cout.width(20);cout << res_meta -> getColumnLabel (i+1);cout.width(20);cout << res_meta -> getColumnTypeName (i+1);cout.width(20);cout << res_meta -> getColumnDisplaySize (i+1) << endl << endl;}cout << "\nColumn \"" << res_meta -> getColumnLabel(1);cout << "\" belongs to the Table: \"" << res_meta -> getTableName(1);cout << "\" which belongs to the Schema: \"" << res_meta -> getSchemaName(1) << "\"" << endl << endl;} // retrieve_rsmetadata_and_print()int main(int argc, const char *argv[]) {Driver *driver;Connection *con;Statement *stmt;ResultSet *res;PreparedStatement *prep_stmt;Savepoint *savept;int updatecount = 0;/* initiate url, user, password and database variables */string url(argc >= 2 ? argv[1] : DBHOST);const string user(argc >= 3 ? argv[2] : USER);const string password(argc >= 4 ? argv[3] : PASSWORD);const string database(argc >= 5 ? argv[4] : DATABASE);try {driver = get_driver_instance();/* create a database connection using the Driver */con = driver -> connect(url, user, password);/* alternate syntax using auto_ptr to create the db connection *///auto_ptr  con (driver -> connect(url, user, password));/* turn off the autocommit */con -> setAutoCommit(0);cout << "\nDatabase connection\'s autocommit mode = " << con -> getAutoCommit() << endl;/* select appropriate database schema */con -> setSchema(database);/* retrieve and display the database metadata */retrieve_dbmetadata_and_print (con);/* create a statement object */stmt = con -> createStatement();cout << "Executing the Query: \"SELECT * FROM City\" .." << endl;/* run a query which returns exactly one result set */res = stmt -> executeQuery ("SELECT * FROM City");cout << "Retrieving the result set .." << endl;/* retrieve the data from the result set and display on stdout */retrieve_data_and_print (res, NUMOFFSET, 1, string("CityName"));/* retrieve and display the result set metadata */retrieve_rsmetadata_and_print (res);cout << "Demonstrating Prepared Statements .. " << endl << endl;/* insert couple of rows of data into City table using Prepared Statements */prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");cout << "\tInserting \"London, UK\" into the table, City .." << endl;prep_stmt -> setString (1, "London, UK");updatecount = prep_stmt -> executeUpdate();cout << "\tCreating a save point \"SAVEPT1\" .." << endl;savept = con -> setSavepoint ("SAVEPT1");cout << "\tInserting \"Paris, France\" into the table, City .." << endl;prep_stmt -> setString (1, "Paris, France");updatecount = prep_stmt -> executeUpdate();cout << "\tRolling back until the last save point \"SAVEPT1\" .." << endl;con -> rollback (savept);con -> releaseSavepoint (savept);cout << "\tCommitting outstanding updates to the database .." << endl;con -> commit();cout << "\nQuerying the City table again .." << endl;/* re-use result set object */res = NULL;res = stmt -> executeQuery ("SELECT * FROM City");/* retrieve the data from the result set and display on stdout */retrieve_data_and_print (res, COLNAME, 1, string ("CityName"));cout << "Cleaning up the resources .." << endl;/* Clean up */delete res;delete stmt;delete prep_stmt;con -> close();delete con;} catch (SQLException &e) {cout << "ERROR: SQLException in " << __FILE__;cout << " (" << __func__<< ") on line " << __LINE__ << endl;cout << "ERROR: " << e.what();cout << " (MySQL error code: " << e.getErrorCode();cout << ", SQLState: " << e.getSQLState() << ")" << endl;if (e.getErrorCode() == 1047) {/*Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)Message: Unknown command*/cout << "\nYour server does not seem to support Prepared Statements at all. ";cout << "Perhaps MYSQL < 4.1?" << endl;}return EXIT_FAILURE;} catch (std::runtime_error &e) {cout << "ERROR: runtime_error in " << __FILE__;cout << " (" << __func__ << ") on line " << __LINE__ << endl;cout << "ERROR: " << e.what() << endl;return EXIT_FAILURE;}return EXIT_SUCCESS;} // main()# CC -VCC: Sun C++ 5.9 SunOS_i386 Patch 124864-09 2008/12/16# CC -o mysqlconnectorc++client -g0 -xO4 -features=extensions -I/opt/coolstack/mysql_32bit/include/mysql -I/export/expts/MySQLConnectorC++/include/cppconn -L/opt/coolstack/mysql_32bit/lib/mysql -L/export/expts/MySQLConnectorC++/lib -lmysqlclient_r -lmysqlcppconn MySQLConnectorC++Client.cpp# export LD_LIBRARY_PATH=/opt/coolstack/mysql_32bit/lib/mysql:/export/expts/ConnectorC++/lib/:$LD_LIBRARY_PATH# ./mysqlconnectorc++client localhost root admin testDatabase connection's autocommit mode = 0Database Metadata-----------------Database Product Name: MySQLDatabase Product Version: 5.1.24-rc-standardDatabase User Name: root@localhostDriver name: MySQL Connector/C++Driver version: 1.0.5Database in Read-Only Mode?: falseSupports Transactions?: trueSupports DML Transactions only?: falseSupports Batch Updates?: trueSupports Outer Joins?: trueSupports Multiple Transactions?: trueSupports Named Parameters?: falseSupports Statement Pooling?: falseSupports Stored Procedures?: trueSupports Union?: trueMaximum Connections: 151Maximum Columns per Table: 512Maximum Columns per Index: 16Maximum Row Size per Table: 2147483639 bytesDatabase schemas:Total number of schemas = 41. information_schema2. ISVe3. mysql4. testExecuting the Query: "SELECT * FROM City" ..Retrieving the result set ..Retrieved 3 row(s).CityName--------Hyderabad, IndiaSan Francisco, USASydney, AustraliaResultSet Metadata------------------Number of columns in the result set = 1Column Name/Label         Column Type         Column SizeCityName             VARCHAR                  30Column "CityName" belongs to the Table: "City" which belongs to the Schema: "test"Demonstrating Prepared Statements ..Inserting "London, UK" into the table, City ..Creating a save point "SAVEPT1" ..Inserting "Paris, France" into the table, City ..Rolling back until the last save point "SAVEPT1" ..Committing outstanding updates to the database ..Querying the City table again ..Retrieved 4 row(s).CityName--------Hyderabad, IndiaSan Francisco, USASydney, AustraliaLondon, UKCleaning up the resources ..

Some of the important steps in the above code sample are explained below.

Establish a Connection to the MySQL Server

A connection to the MySQL Server is established by retrieving an instance of sql::Connection from the sql::Driver object. An sql::Driver object is returned by the sql::Driver::get_driver_instance method, and sql::Driver::connect method returns the sql::Connection object.

Note:

:::: notation was used in the above paragraph to show the fully qualified method name. For example, in sql::Driver::get_driver_instance(), sql is the namespace, Driver is the class name and get_driver_instance() is the name of the method. In C++ applications that use Connector/C++, you could include "using namespace sql;" at the top of your code to avoid having to prefix all Connector/C++ declarations with "sql::". For the remainder of this tutorial, the namespace, sql, was omitted from all Connector/C++ specific declarations in favor of simplicity.

Signatures for Driver::get_driver_instance and Driver::connect methods are shown below. Check the driver.h header in your Connector/C++ installation for the complete list of methods.

/* driver.h */Driver* Driver::get_driver_instance()Connection* Driver::connect(const std::string& URL, const std::string& userName, const std::string& password)Connection* Driver::connect(std::map properties)

The connect method was overloaded in the Driver class. There are two forms of the connect method. In one form, connect accepts a database connection URL along with the database username and the password. The second form accepts a std::map that contains the connection URL, database username and the password as the key/value pairs.

You can connect to the MySQL Server using TCP/IP by specifying "tcp://[hostname[:port]][/schemaname]" for the connection URL. eg., tcp://127.0.0.1:5555/some_schema. Both hostname and the port number are optional, and defaults to 127.0.0.1 and 3306 respectively. During runtime, localhost will automatically be translated to 127.0.0.1. Specifying the schema name in connection URL is optional as well, and if not set, be sure to select the database schema using Connection::setSchema method.

If you want to use UNIX domain socket to connect to the MySQL Server running on the localhost, specify "unix://path/to/unix_socket_file" for the database connection URL. eg., unix:///tmp/mysql.sock.

On Windows, you can use named pipes to connect to the MySQL Server running on the localhost by specifying the string "pipe://path/to/the/pipe" for the database connection URL. To enable the support for named pipes, you must start the MySQL Server with the --enable-named-pipe option. If you don't specify a name to the pipe using the server option --socket=name, a named pipe with the default name MySQL is created. The name of the pipe is case insensitive on Microsoft Windows.

The following code snippet attempts to connect to the MySQL Serverrunning on the localhost on the default port 3306, using the databaseusername root, password admin and schema name test.

using namespace sql;Driver *driver;Connection *con;try {driver = get_driver_instance();con = driver -> connect("tcp://127.0.0.1:3306/test", "root", "admin");} catch (..) {..}

connect can also be called as shown below using the second form of the overloaded method. ConnectPropertyVal is of type union, defined in connection.h header. Include the header when compiling the example with this alternate code.

..std::map conn_properties;ConnectPropertyVal tmp;tmp.str.val = "unix:///tmp/mysql.sock";conn_properties [std::string("hostName")] = tmp;tmp.str.val = "root";conn_properties [std::string("userName")] = tmp;tmp.str.val = "admin";conn_properties [std::string("password")] = tmp;try {driver = get_driver_instance();con = driver -> connect(conn_properties);} catch(..) {..}

If you prefer to separate the protocol from the path to the UNIXsocket in the above code, rewrite the line that has the databaseconnection URL as shown below.
            tmp.str.val = "unix://" "/tmp/mysql.sock";

Once the connection has been established, you can use Connection::setSessionVariable method for setting variables like sql_mode.

C++ Specific Note

The C++ statement sql::Connection *con = driver -> connect("tcp://127.0.0.1:3306", "root", "admin"); can be re-written using auto_ptr template class as shown below.

std::auto_ptr < sql::Connection > con ( driver -> connect("tcp://127.0.0.1:3306", "root", "admin") );= OR =use namespace std;use namespace sql;auto_ptr < Connection > con ( driver -> connect("tcp://127.0.0.1:3306", "root", "admin") );

The C++ standard library class template auto_ptr helpsdevelopers manage dynamic memory, and prevent memory leaks in case ofunexpected events such as exceptions which would cause the normalcleanup code to be skipped. An auto_ptr object has thesame semantics as that of a pointer - however when it goes out ofscope, it automatically releases the dynamic memory it manages. Thatis, when auto_ptr template class is in use, you need not free up the memory explicitly using the delete operator. eg., delete con;

To take advantage of auto_ptr, you will need to include the header. This will give access to the std namespace, in which the template class auto_ptr<type> resides. The type is the data/object type you want your pointer to point to.

The choice of adopting the auto_ptr smart pointer mechanism or the traditional mechanism for the dynamic memory management is left to reader's discretion.

Obtain a Statement Object

When the Connection::createStatement method is called, it returns a Statementobject that could be used to send SQL statements to the databaseserver. SQL statements without parameters are normally executed using Statement objects. In other words, a Statementobject is used to execute a static SQL statement and to return theresults that it produces. If the same SQL statement has to be executedmultiple times with different inputs, consider using Prepared Statements for efficiency during the runtime.

Signature for the Connection::createStatement method is shown below. For the complete list of methods supported by the Connection interface, check the connection.h header in your Connector/C++ installation.

/* connection.h */Statement* Connection::createStatement();

The following code fragment invokes the createStatement method of the Connection interface to obtain an object of type Statement.

Connection *con;Statement *stmt;Statement stmt = con -> createStatement();

In this example, con is a reference to an object of type Connection.

Execute the SQL Statements

Before you can execute the SQL statements over the databaseconnection, you have to select appropriate database schema. To selectthe database schema, call the setSchema method of the Connection object with the schema name as the argument.

To execute the SELECT statements, call the Statement::executeQuery method with the SQL statement as the argument. executeQuery() returns a ResultSet object. The Statement::executeUpdate method can be used to execute the given SQL statement, which may be an INSERT, UPDATE, DELETE, or any SQL statement that returns nothing, such as an SQL DDL statement. Unlike executeQuery(), the executeUpdate method does not return a ResultSet. Instead, it returns the number of rows affected by the INSERT, UPDATE, or DELETE statements.

If you don't know ahead of time whether the SQL statement will be a SELECT or an INSERT, UPDATE, or DELETE, use the execute method. execute() returns true if the SQL query was a SELECT, and returns false if the statement was an INSERT, UPDATE, or DELETE. If the statement was a SELECT query, you can retrieve the results by calling the getResultSet method on the Statement instance. If the statement was an INSERT, UPDATE, or DELETE statement, you can retrieve the count of affected rows by calling getUpdateCount().

In some uncommon situations, a single SQL statement may returnmultiple result sets and/or update counts. Normally you can ignore thisunless you are executing a stored procedure that you know may returnmultiple results or you are dynamically executing an unknown SQLstatement. In such cases, use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults() to determine whether there is another result set.

Signatures for some of the relevant methods are shown below. For the complete list of methods supported by Connection and Statement interfaces, check the connection.h and statement.h headers in your Connector/C++ installation.

/* connection.h */void Connection::setSchema(const std::string& catalog);/* statement.h */ResultSet* Statement::executeQuery (const std::string& sql);int Statement::executeUpdate (const std::string& sql);bool Statement::execute (const std::string& sql);ResultSet* Statement::getResultSet();uint64_t Statement::getUpdateCount();

All the above methods throw the SQLException, so makesure to catch those exceptions in your code. Just for the sake ofsimplicity, the code fragments in the examples are not surrounded bythe try .. catch blocks.

If you look again at the full sample code, you will see that the objective is to retrieve all the rows from the City table in the test database. Hence executeQuery() was used in the sample code as shown here:

Statement *stmt;ResultSet *res;res = stmt -> executeQuery ("SELECT * FROM City");

The method executeQuery returns a ResultSet object that contains the data generated by the given query. This method throws SQLException in case of a database access error or if this method is called on a closed Statement or the given SQL statement produces anything other than a single ResultSet object.

Alternatively, the above code fragment can be re-written using Statement::execute() as shown below.

bool retvalue = stmt -> execute ("SELECT * FROM City");if (retvalue) {res = stmt -> getResultSet();} else {...}

The method execute returns true if the first result is a ResultSet object, and false if it is an update count or there are no results. If execute() returns true, retrieve the result set using the getResultSet method. getResultSet() returns NULL if the result is an update count or if there are no more results. getResultSet method should be called only once per result.

Both the methods, execute and getResultSet, throw the SQLException in case of a database access error or if this method is called on a closed Statement.

If you want to insert new records into the database, you can do so with the help of executeUpdate method as shown in the following example:

int updateCount = stmt -> executeUpdate ("INSERT INTO City (CityName) VALUES ('Napier, New Zealand')");

The method executeUpdate executes the given SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE, or an SQL statement that returns nothing, such as a DDL statement. executeUpdate() returns the row count for INSERT, UPDATE or DELETE statements and 0 for SQL statements that return nothing.

executeUpdate() throws SQLException in case of adatabase access error or when it is called on a closed Statement or thegiven SQL statement produces a ResultSet object.

Another way to re-write the above code fragment using execute and getUpdateCount methods:

int updateCount = 0;bool retstatus = stmt -> execute ("INSERT INTO City (CityName) VALUES ('Napier, New Zealand')");if (!retstatus) {updateCount = stmt -> getUpdateCount();} else {...}

The method execute returns false if thefirst result is an update count or if there are no results. If thefirst result is an update count , retrieve the value using the getUpdateCount method. getUpdateCount() returns -1 if the current result is a ResultSet object or if there are no more results. getUpdateCount() should be called only once per result.

Both the methods, execute and getUpdateCount, throw the SQLException in case of a database access error or if called on a closed Statement.

Retrieve the Data From the Result Set

The previous paragraphs explained that the methods for executing the SQL queries, executeQuery and execute, return an instance of ResultSet. You can use the ResultSet object to access the data that was returned by running a query against the database. Every ResultSetobject maintains a cursor, which points to its current row of data. Therows in the result set cursor are retrieved in sequence. Within a row,column values can be accessed in any order. You can refer to thecolumns by their position (offset) or by their name/label, though thelatter is fault-tolerant to some extent, especially when there arechanges to the table schema. Reference by the column label or namemakes the code lucid. On the other hand, using the column index, orpositional reference, may improve the performance.

Column label is the label for the column specified with the SQL "AS"clause. If the SQL "AS" clause is not specified, then the label is thename of the column. For example, CN is the column label in the SQL statement: SELECT CityName AS CN FROM City.

The data stored in the ResultSet can be retrieved by using various getXX methods such as getString() or getInt(), depending on the type of data being retrieved. Use the next and previous methods of the ResultSet object to move the cursor to the next and previous rows in the result set.

The ResultSet object remains open even when the Statementobject that generated it is closed, re-executed, or used to retrievethe next result from a sequence of multiple results. Once the resultset had been pulled out of the Statement, the ResultSet object remains valid until it is closed explicitly or implicitly, irrespective of the state of the Statement object that generated it.

As of this writing, MySQL Connector/C++ returns buffered results for the Statementobjects. Buffered result sets are cached on the client. The driver willalways fetch all the data no matter how big the result set is. Futureversions of the connector is expected to return buffered and unbufferedresults for the Statement objects.

Signatures for some of the relevant methods are shown below. For the complete list of methods supported by the ResultSet interface, check the resultset.h headers in your Connector/C++ installation.

/* resultset.h */size_t ResultSet::rowsCount() const;void ResultSet::close();bool ResultSet::next();bool ResultSet::previous();bool ResultSet::last();bool ResultSet::first();void ResultSet::afterLast();void ResultSet::beforeFirst();bool ResultSet::isAfterLast() const;bool ResultSet::isBeforeFirst()const;bool ResultSet::isClosed() const;bool ResultSet::isNull(uint32_t columnIndex) const;bool ResultSet::isNull(const std::string& columnLabel) const;bool ResultSet::wasNull() const;std::string ResultSet::getString(uint32_t columnIndex) const;std::string ResultSet::getString(const std::string& columnLabel) const;int32_t ResultSet::getInt(uint32_t columnIndex) const;int32_t ResultSet::getInt(const std::string& columnLabel) const;

In the sample C++ code, the query "SELECT * FROM City" returns a ResultSet with only one column CityName of data type String, known as VARCHAR in MySQL.

The following code fragment loops through the ResultSet object res, retrieves the CityName from each row by referencing the exact column name, and displays it on the standard output:

while (res -> next()) {cout << rs -> getString("CityName") << endl;}

Because it is also possible to refer to the column by its position, the following code fragment produces similar results:

while (res -> next()) {cout << rs -> getString(1) << endl;}

The integer argument to getString() refers to the position of the column in the list of columns specified in the query, starting with 1 for the first field.

Both versions of getString() return the column value. If the column value is SQL NULL, the value returned is an empty string. You can use ResultSet::isNullmethod with the column offset or column name/label as the argument tocheck whether the column value being fetched is a SQL NULL. Todetermine whether the last column read had a value of SQL NULL, call ResultSet::wasNULL() with no arguments.

The following example shows how to fetch the data by traversing the cursor in reverse order.

/* Move the cursor to the end of the ResultSet object, just after the last row */res -> afterLast();if (!res -> isAfterLast()) {throw runtime_error("Error: Cursor position should be at the end of the result set after the last row.");}/* fetch the data : retrieve all the rows in the result set */while (res -> previous()) {cout << rs -> getString("CityName") << endl;}

The method getString throws SQLExceptionif the column label/name is not valid or if the column index is notvalid, in case of a database access error or if this method is calledon a closed result set.

Using Prepared Statements

MySQL 4.1 introduced prepared statements to accomplish the task ofexecuting a query repeatedly, albeit with different parameters in eachiteration. Prepared Statements can help increase security by separatingSQL logic from the data being supplied. This separation of logic anddata can help prevent a very common type of vulnerability known as SQLinjection attack. However note that although prepared statements canimprove the security, it is still the responsibility of the applicationdeveloper to guard against security attacks and to sanitize the inputbefore submitting to the database for processing.

Prepared Statements are optimized for handling parameterized SQL statements that can benefit from pre-compilation. Unlike Statement object, an SQL statement is provided to the PreparedStatementobject when it is created. In most cases, the SQL statement is sent tothe database server right away, where the query parser performs thesyntax check, syntactic optimization and finally parses (compiles) theSQL for later use by the MySQL Server . As a result, the PreparedStatementobject contains an SQL statement that has been precompiled. This meanswhen the prepared statement is executed, the database server can justrun the PreparedStatement SQL statement without having tocompile it first. Reduced query parsing may lead to significantperformance improvements in the MySQL Server.

The MySQL client/server protocol supports two methods of sending thedatabase results to the client: as text and as binary. The textprotocol always converts the data into strings before sending themacross the network, and the server decodes the strings into appropriatedata types. Unlike the text protocol, the binary protocol avoidsconverting the data into strings wherever possible. The binary protocolis used only with the prepared statements. Based on the data being sentover the network, the binary protocol that the prepared statements usecan reduce the CPU and network overhead by eliminating the encoding anddecoding of the strings into correct data types at the client and theserver.

Connector/C++ is based on the MySQL C API and the C library libmysql.Therefore it inherits all limitations from the MySQL Server and theMySQL C API. The following statements can be used as preparedstatements with Connector/C++: CALL, CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, and majority of the SHOW statements. USE is not supported by the prepared statement protocol, and Connector/C++ does not include a prepared statement emulation for the USE statement. Check the MySQL C API Prepared Statements documentation for the complete list of statements which can be prepared.

Although PreparedStatement objects can be used with SQL statements with no parameters, you probably use them often with parameterized SQL statements.

Create a PreparedStatement Object

Just like Statement objects, you can create PreparedStatement objects by using the Connection instance. Calling the prepareStatement method against an active database Connection object creates a PreparedStatement object for sending parameterized SQL statements to the database.

Method signature:

/* connection.h */PreparedStatement * Connection::prepareStatement(const std::string& sql) throws SQLException;

prepareStatement() returns a new default PreparedStatement object containing the pre-compiled SQL statement. This method throws SQLException in case of a database access error or when this method is called on a closed connection.

The following code fragment in the sample C++ code creates the PreparedStatement object.

Connection *con;PreparedStatement *prep_stmt;..prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");

Supply Values for PreparedStatement Parameters

In case of parameterized SQL statements, you need to supply thevalues to be used in place of the question mark placeholders before youcan execute the SQL statement. You can do this by calling one of the setXX methods defined in the PreparedStatement class. The setter methods (setXX())for setting IN parameter values must specify types that are compatiblewith the defined SQL type of the input parameter. For instance, if theIN parameter has SQL type INTEGER, then setInt() should be used.

The following code fragment in the sample C++ code sets the question mark placeholder to a C++ std::string with a value of "London, UK".

PreparedStatement *prep_stmt;..prep_stmt -> setString (1, "London, UK");

Execute the SQL statement in the PreparedStatement Object

Similar to the execution of SQL statements with the Statement object, you can use executeQuery method to execute SELECT statements, executeUpdate method to execute SQL statements which may be an INSERT, UPDATE, DELETE or an SQL statement that returns nothing such as an SQL DDL statement, and execute method to execute SQL statement of any kind. Check the Execute the SQL Statements paragraph for more details.

Signatures for some of the relevant methods are shown below. For the complete list of methods supported by PreparedStatement interface, check the prepared_statement.h header in your Connector/C++ installation.

/* prepared_statement.h */ResultSet* PreparedStatement::executeQuery();int PreparedStatement::executeUpdate();bool PreparedStatement::execute();ResultSet* PreparedStatement::getResultSet();uint64_t PreparedStatement::getUpdateCount();

All the above methods throw the SQLException, so make sure to catch those exceptions in your code.

The ResultSet object remains open even when the PreparedStatementobject that generated it is re-executed, or used to retrieve the nextresult from a sequence of multiple results. Once the result set hadbeen pulled out of the PreparedStatement, the ResultSet object remains valid until it is closed explicitly or implicitly, or the PreparedStatement object that generated it is closed, whichever occurs first.

The following code fragment in the sample C++ code executes the SQL statement in the PreparedStatement object.

..prep_stmt -> setString (1, "London, UK");int updatecount = prep_stmt -> executeUpdate();

In general, parameter values remain in force for repeated use of astatement. Setting a parameter value automatically clears its previousvalue. However, in some cases it is useful to release all the resourcesused by the current parameter values at once. This can be done bycalling the method, PreparedStatement::clearParameters.

Using Transactions

A database transaction is a set of one or more statements that areexecuted together as a unit, so either all of the statements areexecuted, or none of them are executed. MySQL supports localtransactions within a given client session through statements such as SET autocommit, START TRANSACTION, COMMIT, and ROLLBACK.

Disable AutoCommit Mode

By default, all the new database connections are in autocommit mode.In the autocommit mode, all the SQL statements will be executed andcommitted as individual transactions. The commit occurs when thestatement completes or the next execute occurs, whichever comes first.In case of statements that return a ResultSet object, the statement completes when the last row of the ResultSet object has been retrieved or the ResultSet object has been closed.

One way to allow multiple statements to be grouped into atransaction is to disable autocommit mode. In other words, to usetransactions, the Connection object must not be in autocommit mode. The Connection class provides the setAutoCommit method to enable or disable the autocommit. An argument of 0 to setAutoCommit() disables the autocommit, and a value of 1 enables the autocommit.

Connection *con;../* disable the autocommit */con -> setAutoCommit(0);

It is suggested to disable autocommit only while you want to be intransaction mode. This way, you avoid holding database locks formultiple statements, which increases the likelihood of conflicts withother users.

Commit or Rollback a Transaction

Once autocommit is disabled, changes to transaction-safe tables suchas those for InnoDB and NDBCLUSTER are not made permanent immediately.You must explicitly call the method commit to make the changes permanent in the database or the method rollback to undo the changes. All the SQL statements executed after the previous call to commit() are included in the current transaction and committed together or rolled back as a unit.

The following code fragment, in which con is an active connection, illustrates a transaction.

Connection *con;PreparedStatement *prep_stmt;..con -> setAutoCommit(0);prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");prep_stmt -> setString (1, "London, UK");prep_stmt -> executeUpdate();con -> rollback();prep_stmt -> setString (1, "Paris, France");prep_stmt -> executeUpdate();con -> commit();

In this example, autocommit mode is disabled for the connection con, which means that the prepared statement prep_stmt is committed only when the method commitis called against this active connection object. In this case, anattempt has been made to insert two rows into the database using theprepared statement, but the first row with data "London, UK" was discarded by calling the rollback method while the second row with data "Paris, France" was inserted into the City table by calling the commit method.

Another example to show the alternate syntax to disable the autocommit, then to commit and/or rollback transactions explicitly.

Connection *con;Statement *stmt;..stmt = con -> createStatement();//stmt -> execute ("BEGIN;");//stmt -> execute ("BEGIN WORK;");stmt -> execute ("START TRANSACTION;");stmt -> executeUpdate ("INSERT INTO City (CityName) VALUES ('London, UK')");stmt -> execute ("ROLLBACK;");stmt -> executeUpdate ("INSERT INTO City (CityName) VALUES ('Paris, France')");stmt -> execute ("COMMIT;");

The START TRANSACTION or BEGIN statement starts a new transaction. COMMIT commits the current transaction to the database by making the changes permanent. ROLLBACK rolls back the current transaction by canceling the changes to the database. With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.

BEGIN and BEGIN WORK are supported as aliases of START TRANSACTION for initiating a transaction. START TRANSACTION is standard SQL syntax and it is the recommended way to start an ad-hoc transaction.

Rollback to a Savepoint within a Transaction

The MySQL connector for C++ supports setting savepoints with the help of Savepoint class, which offer finer control within transactions. The Savepointclass allows you to partition a transaction into logical breakpoints,providing control over how much of the transaction gets rolled back.

As of this writing, InnoDB and Falcon storage engines support the savepoint transactions in MySQL 6.0.

To use transaction savepoints, the Connection objectmust not be in autocommit mode. When the autocommit is disabled,applications can set a savepoint within a transaction and then rollback all the work done after the savepoint. Note that enablingautocommit invalidates all the existing savepoints, and theConnector/C++ driver throws an InvalidArgumentException when an attempt has been made to roll back the outstanding transaction until the last savepoint.

A savepoint is either named or unnamed. You can specify a name to the savepoint by supplying a string to the Savepoint::setSavepoint method. If you do not specify a name, the savepoint is assigned an integer ID. You can retrieve the savepoint name using Savepoint::getSavepointName().

Signatures of some of the relevant methods are shown below. For the complete list of methods supported by Connection, Statement, PreparedStatement and Savepoint interfaces, check the connection.h, statement.h and prepared_statement.h headers in your Connector/C++ installation.

/* connection.h */Savepoint* Connection::setSavepoint(const std::string& name);void Connection::releaseSavepoint(Savepoint * savepoint);void Connection::rollback(Savepoint * savepoint);

The following code fragment inserts a row into the table City, creates a savepoint SAVEPT1, then inserts a second row. When the transaction is later rolled back to SAVEPT1,the second insertion is undone, but the first insertion remains intact.In other words, when the transaction is committed, only the rowcontaining "London, UK" will be added to the table City.

Connection *con;PreparedStatement *prep_stmt;Savepoint *savept;..prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");prep_stmt -> setString (1, "London, UK");prep_stmt -> executeUpdate();savept = con -> setSavepoint ("SAVEPT1");prep_stmt -> setString (1, "Paris, France");prep_stmt -> executeUpdate();con -> rollback (savept);con -> releaseSavepoint (savept);con -> commit();

The method Connection::releaseSavepoint takes a Savepointobject as a parameter and removes it from the current transaction. Anysavepoints that have been created in a transaction are automaticallyreleased and become invalid when the transaction is committed, or whenthe entire transaction is rolled back. Rolling back a transaction to asavepoint automatically releases and invalids any other savepoints thatwere created after the savepoint in question. Once a savepoint has beenreleased, any attempt to reference it in a rollback operation causesthe SQLException to be thrown.

Accessing Result Set Metadata

When the SQL statement being processed is unknown until runtime, the ResultSetMetaData interface can be used to determine the methods to be used to retrieve the data from the result set. ResultSetMetaData provides information about the structure of a given result set. Data provided by the ResultSetMetaDataobject includes the number of columns in the result set, the names orlabels and the types of those columns along with the attributes of eachcolumn and the names of the table, schema and the catalog thedesignated column's table belongs to.

When getMetaData() is called on a ResultSet object, it returns a ResultSetMetaData object describing the columns of that ResultSet object.

Signatures of some of the relevant methods are shown below. For the complete list of methods supported by ResultSetMetaData interface, check the resultset_metadata.h header in your Connector/C++ installation.

/* resultset.h */ResultSetMetaData * ResultSet::getMetaData() const;/* prepared_statement.h */ResultSetMetaData * PreparedStatement::getMetaData() const;/* resultset_metadata.h */std::string ResultSetMetaData::getCatalogName(unsigned int columnIndex);std::string ResultSetMetaData::getSchemaName(unsigned int columnIndex);std::string ResultSetMetaData::getTableName(unsigned int columnIndex);unsigned int ResultSetMetaData::getColumnCount();unsigned int ResultSetMetaData::getColumnDisplaySize(unsigned int columnIndex);std::string ResultSetMetaData::getColumnLabel(unsigned int columnIndex);std::string ResultSetMetaData::getColumnName(unsigned int columnIndex);int ResultSetMetaData::getColumnType(unsigned int columnIndex);std::string ResultSetMetaData::getColumnTypeName(unsigned int columnIndex);int ResultSetMetaData::isNullable(unsigned int columnIndex);bool ResultSetMetaData::isReadOnly(unsigned int columnIndex);bool ResultSetMetaData::isWritable(unsigned int columnIndex);

The following code fragment demonstrates how to retrieve all thecolumn names or labels, their data types and the sizes along with thetable name and the schema names to which they belong.

ResultSet *rs;ResultSetMetaData *res_meta;res_meta = rs -> getMetaData();int numcols = res_meta -> getColumnCount();cout << "\nNumber of columns in the result set = " << numcols << endl;cout.width(20);cout << "Column Name/Label";cout.width(20);cout << "Column Type";cout.width(20);cout << "Column Size" << endl;for (int i = 0; i < numcols; ++i) {cout.width(20);cout << res_meta -> getColumnLabel (i+1);cout.width(20);cout << res_meta -> getColumnTypeName (i+1);cout.width(20);cout << res_meta -> getColumnDisplaySize (i+1) << endl;}cout << "\nColumn \"" << res_meta -> getColumnLabel(1);cout << "\" belongs to the Table: \"" << res_meta -> getTableName(1);cout << "\" which belongs to the Schema: \"" << res_meta -> getSchemaName(1) << "\"" << endl;//delete res_meta;delete rs;

From release 1.0.5 onwards, the connector takes care of cleaning the ResultSetMetaData objects automatically when they go out of scope. This will relieve the clients from deleting the ResultSetMetaData objects explicitly. Due to the implicit destruction of the metadata objects, clients won't be able to delete the ResultSetMetaData objects directly. Any attempt to delete the ResultSetMetaData object results in compile time error. Similarly using auto_ptr template class to instantiate an object of type ResultSetMetaDataresults in compile time error. For example, compilation of the abovecode fails with Connector/C++ 1.0.5 and later versions, when thestatement delete res_meta; is uncommented.

Prepared Statements and the Result Set Metadata

PreparedStatement::getMetaData() retrieves a ResultSetMetaData object that contains information about the columns of the ResultSet object, which will be returned when the PreparedStatement object is executed.

Because a PreparedStatement object is precompiled, it is possible to know about the ResultSet object that it will return without having to execute it. Consequently, it is possible to invoke the method getMetaData on a PreparedStatement object rather than waiting to execute it and then invoking the ResultSet::getMetaData method on the ResultSet object that is returned.

The method PreparedStatement::getMetaData is supported only in Connector/C++ 1.0.4 and later versions.

Accessing Database Metadata

You can retrieve general information about the structure of a database with the help of DatabaseMetaData interface. DatabaseMetaData exposes a significant amount of information about the support and contents of a given database. For instance, by using the DatabaseMetaDatainterface, it is possible to find out whether the database supportstransactions, outer joins are supported and to what extent, the maximumnumber of allowed concurrent connections to remain open, and the ResultSet types supported.

A user for this interface is commonly a tool that needs to discoverhow to deal with the underlying Database Management System (DBMS). Thisis especially true for applications that are intended to be used withmore than one DBMS. For example, a tool might use getTypeInfo() to find out what data types can be used in a CREATE TABLE statement. A user may call supportsCorrelatedSubqueries() and supportsBatchUpdates() to see if it is possible to use a correlated subquery and if batch updates are allowed.

Database metadata is associated with a particular connection, so objects of the DatabaseMetaData class are created by calling the getMetaData method on an active Connection object.

Signatures of some of the methods in DatabaseMetaData interface are shown below. For the complete list of methods supported by DatabaseMetaData interface, check the metadata.h header in your Connector/C++ installation.

/* connection.h */DatabaseMetaData *Connection::getMetaData();/* metadata.h */const std::string& DatabaseMetaData::getDatabaseProductName();std::string DatabaseMetaData::getDatabaseProductVersion();const std::string& DatabaseMetaData::getDriverName();const std::string& DatabaseMetaData::getDriverVersion();ResultSet *DatabaseMetaData::getTables(const std::string& catalog, const std::string& schemaPattern,const std::string& tableNamePattern, std::list &types);ResultSet *DatabaseMetaData::getIndexInfo(const std::string& catalog, const std::string& schema,const std::string& table, bool unique, bool approximate);int DatabaseMetaData::getMaxColumnsInIndex();int DatabaseMetaData::getMaxColumnsInTable();int DatabaseMetaData::getMaxConnections();ResultSet *DatabaseMetaData::getSchemas();ResultSet *DatabaseMetaData::getTableTypes();bool DatabaseMetaData::supportsTransactions();bool DatabaseMetaData::supportsBatchUpdates();bool DatabaseMetaData::supportsMultipleResultSets();bool DatabaseMetaData::supportsNamedParameters();bool DatabaseMetaData::supportsSavepoints();bool DatabaseMetaData::supportsStatementPooling();bool DatabaseMetaData::supportsStoredProcedures();bool DatabaseMetaData::supportsUnion();

The following code fragment demonstrates how to retrieve some of themetadata from the database using the methods outlined above.

Connection *dbcon;/* the following line results in compilation error with Connector/C++ 1.0.5 and later *///auto_ptr < DatabaseMetaData > dbcon_meta ( dbcon -> getMetaData() );DatabaseMetaData *dbcon_meta = dbcon -> getMetaData();cout << boolalpha;cout << "Database Product Name: " << dbcon_meta -> getDatabaseProductName() << endl;cout << "Database Product Version: " << dbcon_meta -> getDatabaseProductVersion() << endl;cout << "Driver name: " << dbcon_meta -> getDriverName() << endl;cout << "Driver version: " << dbcon_meta -> getDriverVersion() << endl << endl;cout << "Supports Transactions?: " << dbcon_meta -> supportsTransactions() << endl;cout << "Supports Named Parameters?: " << dbcon_meta -> supportsNamedParameters() << endl;cout << "Maximum Connections: " << dbcon_meta -> getMaxConnections() << endl;cout << "Maximum Columns per Table: " << dbcon_meta -> getMaxColumnsInTable() << endl;cout << "\nDatabase schemas: " << endl;auto_ptr < ResultSet > rs ( dbcon_meta -> getSchemas() );cout << "\nTotal number of schemas = " << rs -> rowsCount() << endl;int row = 1;while (rs -> next()) {cout << "\t" << row << ". " << rs -> getString("TABLE_SCHEM") << endl;++row;} // while

From release 1.0.5 onwards, the connector takes care of cleaning the DatabaseMetaData objects automatically when they go out of scope. This will relieve the clients from deleting the DatabaseMetaData objects explicitly. Due to the implicit destruction of the metadata objects, clients won't be able to delete the DatabaseMetaData objects directly. Any attempt to delete the DatabaseMetaData object results in compile time error. Similarly using auto_ptr template class to instantiate an object of type DatabaseMetaData results in compile time error.

Accessing Parameter Metadata from a PreparedStatement Object

Connector/C++ has partial support for returning parameter metadata for a given PreparedStatement object. As of this writing, Connector/C++ has support for only one method, getParameterCount, in the ParameterMetaData interface. Future versions of Connector/C++ may support majority of the methods defined in the ParameterMetaData interface in JDBC 4.0 specification.

The method getParameterCount returns the number of parameters in the PreparedStatement object for which the ParameterMetaData object would contain information. getParameterCount() throws SQLException in case of a database access error.

Signatures of the relevant methods are shown below.

/* prepared_statement.h */ParameterMetaData* PreparedStatement::getParameterMetaData();/* parameter_metadata.h */int ParameterMetaData::getParameterCount();

The method getParameterCount was declared inparameter_metadata.h header in your Connector/C++ installation. Makesure to include parameter_metadata.h header file in your C++application, if your code has references to the ParameterMetaData::getParameterCount method.

The following code fragment demonstrates how to retrieve the parameter count from a given PreparedStatement object.

#include #include #include ......Driver *driver;Connection    *con;PreparedStatement *prep_stmt;ParameterMetaData *param_meta;try {driver = get_driver_instance();con = driver -> connect("tcp://127.0.0.1:3306", "root", "admin");prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");param_meta = prep_stmt -> getParameterMetaData();cout << "Number of parameters in the prepared statement = " << param_meta -> getParameterCount() << endl;//delete param_meta;} catch (..) {...}

From release 1.0.5 onwards, the connector takes care of cleaning the ParameterMetaData objects automatically when they go out of scope. This will relieve the clients from deleting the ParameterMetaData objects explicitly. Due to the implicit destruction of the metadata objects, clients won't be able to delete the ParameterMetaData objects directly. Any attempt to delete the ParameterMetaData object results in compile time error. Similarly using auto_ptr template class to instantiate an object of type ParameterMetaDataresults in compile time error. For example, compilation of the abovecode fails with Connector/C++ 1.0.5 and later versions, when thestatement delete param_meta; is uncommented.

Clean Up: Release the System Resources

If an object is no longer needed, destroy that object by using the C++ delete operator. It is not necessary to explicitly release the memory resources, if you are using the auto_ptr template class to manage the memory resources dynamically. Check C++ Specific Note for more details about the auto_ptrsmart pointer. In all other cases, ensure you explicitly release theallocated memory even in the case of runtime errors and exceptions.Failure to do so results in memory leaking.

Similarly free up the system resources by closing files, destroyingthreads, etc., when they are no longer needed in the context of theapplication.

The following code fragment deletes the ResultSet, Statement, PreparedStatement and Connection objects res, stmt, prep_stmt and con respectively.

Connection *con;Statement *stmt;PreparedStatement *prep_stmt;ResultSet *res;ResultSetMetaData *res_meta;DatabaseMetaData *dbcon_meta;delete res;delete stmt;delete prep_stmt;//delete res_meta;//delete dbcon_meta;con -> close();delete con;

Starting with the release of Connector/C++ 1.0.5, clients are nolonger required to destruct the metadata objects directly. The drivertakes care of the metadata object destruction for the clients.

When autocommit is disabled in MySQL, all the uncommittedtransactions will be rolled back automatically if you close thedatabase connection without calling the commit method explicitly.

Catching Exceptions

In addition to the std::runtime_error, the MySQLConnector/C++ can throw four different exceptions that can provideinformation about a database error or other errors.

  1. SQLException, which was derived from std::runtime_error
  2. InvalidArgumentException, which was derived from SQLException
  3. MethodNotImplementedException, which was derived from SQLException, and
  4. InvalidInstanceException, which was derived from SQLException.

All the above exceptions are defined in exception.h header in your Connector/C++ installation.

Since all those exception classes are derived from std::runtime_errordirectly or indirectly, they can return a C-style character stringdescribing the general cause of the current error when the method what is called against an exception object.

The SQLException class has implementation for two other methods - 1. getSQLState(), which returns the current SQL state in the form of a string; and 2. getErrorCode(), which returns an integer error code that corresponds to the MySQL error code. Check MySQL Reference Manual for the Server Error Codes and Messages.

The following outputs demonstrate Connector/C++'s ability to throw exceptions in case of incorrect inputs.

# ./mysqlconnectorc++client localhost giri admin testERROR: SQLException in MySQLConnectorC++Client.cpp (main) on line 255ERROR: Access denied for user 'giri'@'localhost' (using password: YES) (MySQL error code: 1045, SQLState: 28000)# ./mysqlconnectorc++client localhost root admin test2Database connection's autocommit mode = 0ERROR: SQLException in MySQLConnectorC++Client.cpp (main) on line 255ERROR: Unknown database 'test2' (MySQL error code: 1049, SQLState: 42000)# ./mysqlconnectorc++client ben13.sfbay root admin testERROR: SQLException in MySQLConnectorC++Client.cpp (main) on line 255ERROR: Unknown MySQL server host 'ben13.sfbay' (1) (MySQL error code: 2005, SQLState: HY000)When a non-existing column name is specified:# ./mysqlconnectorc++client localhost root admin test...InvalidArgumentException: MySQL_ResultSet::getString: invalid value of 'columnIndex'...

Debug Tracing with MySQL Connector/C++

In case of random problems, debug traces and protocol filesgenerated by the connector driver are more useful for the problemdiagnosis than the traditional tools like a debugger to debug yourclient application.

The MySQL Connector/C++ is capable of generating two types of debug traces.

  1. Traces generated by the debug version of MySQL Client Library, and
  2. Traces generated internally by the Connector/C++ driver

As of this writing, the debug traces can only be activated throughAPI calls in the case of the debug version of MySQL Client Library.Those traces are controlled on a per-connection basis. On the otherhand, connector's internal traces can be activated for the whole clientapplication by setting the environment variable, MYSQLCPPCONN_TRACE_ENABLED, on non-Windows platforms. In both of those cases, you can use Connection::setClientOptions()to activate and deactivate the debug traces selectively for certainfunction calls. By default, the MySQL Client Library trace is alwayswritten to the standard error (stderr), whereas the Connector/C++drivers' protocol messages are written to the standard output (stdout).

Be advised that the debug traces may contain SQL statements fromyour application. Exercise caution when turning the trace functionalityon, especially when the SQL statements should not be exposed at theclient side.

Signature of the setClientOptions method is shown below. This method was defined in connection.h header in your Connector/C++ installation.

/* connection.h */void Connection::setClientOption(const std::string & optionName, const void * optionValue);

The string "libmysql_debug" for the option name enables the debug trace in the case of debug version of MySQL Client Library, where as the string "clientTrace" enables the debug trace in the case of internal tracing by the Connector/C++ driver.

Traces Generated by the MySQL Client Library

The connector driver implicitly calls the C-API function, mysql_debug,to enable this debug trace. Only debug version of the MySQL ClientLibrary is capable of generating the debug trace. Therefore to enablethe trace functionality, link the Connector/C++ driver against thedebug version of the MySQL Client Library. It is not necessary to buildthe driver with -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1 CMake option or to set the environment variable, MYSQLCPPCONN_TRACE_ENABLED,in the runtime to generate this trace. The trace shows the internalfunction calls and the addresses of internal objects as shown below.

mysql_real_query| enter: handle: 0x808a228| query: Query = 'SELECT CityName as CITYNAME FROM City'| >mysql_send_query| | enter: rpl_parse: 0  rpl_pivot: 1| cli_advanced_command| | >net_write_command| | | enter: length: 37| | net_flush| | | net_real_write| | | | >vio_write| | | | | enter: sd: 4  buf: 0x808fa38  size: 42| | | | | exit: 42......Voluntary context switches 25, Involuntary context switches 5>TERMINATE| safe: sf_malloc_count: 4| safe: Memory that was not free'ed (16420 bytes):| safe: Maximum memory usage: 65569 bytes (65k)

The following code fragment demonstrates how to generate debug trace for the executeQuery function call using MySQL Client Library and Connector/C++'s Connection::setClientOptions().

Driver *driver;Connection *con;Statement *stmt;ResultSet *rs;string url, user, password;try {driver = get_driver_instance();con = driver -> connect(url, user, password);stmt = con -> createStatement();con -> setClientOption ("libmysql_debug", "d:t:O,/tmp/client.trace");rs = stmt -> executeQuery ("SELECT * FROM City");con -> setClientOption ("libmysql_debug", "f");} catch (SQLException &e) {..}

The debug control string "d:t:O,/tmp/client.trace" specifies what kind of debug traces to be collected.

The flag 'd' enables output from DBUG_ macros for the current state. In MySQL, common tags to print with the option 'd' are ENTER, EXIT, ERROR, WARNING, INFO, and LOOP. For the complete list of supported DBUG macros, check the DBUG C Program Debugging Package document.

The flag 't' enables function call/exit trace lines. Itcan be be followed by a list giving a numeric maximum trace level,beyond which no output occurs for either debugging or tracing macros.eg., t,20

The flag 'O,/tmp/client.trace' redirects the debugger output stream to the specified file, /tmp/client.trace,on the client machine. The file is flushed between each write. Whenneeded, the file is closed and re-opened between each write.

The flag 'f' followed by an empty list of named functions disables the debug trace.

For the complete list of debug control flags and their descriptions, check the MySQL Reference Manual.

Internal Traces Generated by the Connector/C++ Driver

Connector/C++ driver's internal debug trace is a call trace. Itshows each and every function call and sometimes the function argumentsand other related information. However the internal tracingfunctionality is not available by default with Connector/C++. Whenbuilding the Connector/C++ driver, make sure to enable the tracingmodule by using the CMake option, -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1.It is not necessary to link the application against the debug versionof the MySQL Client Library to generate internal traces ofConnector/C++. Detailed instructions are in the Installing MySQL Connector/C++ section of this tutorial.

Compiling the connector driver with the tracing functionalityenabled will incur two additional tracing function call overhead pereach driver function call as shown below.

...#    MySQL_Connection::setAutoCommit#    MySQL_Connection::getAutoCommitExecuting query: SELECT * FROM City ..#    >MySQL_Statement::execute#    |  INF: this=80694b8#    |  INF: query=START TRANSACTION;#    |  >MySQL_Statement::do_query#    |  |  INF: this=80694b8#    |  MySQL_Statement::execute#    |  INF: this=80694b8#    |  INF: query=INSERT INTO City (CityName) VALUES ('Napier, New Zealand')#    |  >MySQL_Statement::do_query#    |  |  INF: this=80694b8#    |  MySQL_Statement::getUpdateCount#    MySQL_Statement::execute#    |  INF: this=80694b8#    |  INF: query=ROLLBACK;#    |  >MySQL_Statement::do_query#    |  |  INF: this=80694b8...

When the Connector/C++ driver is built with the CMake option, -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1, users on non-Windows platforms can enable the debug tracing for the whole application by setting the environment variable MYSQLCPPCONN_TRACE_ENABLED to any value on the command line prior to running the C++ client application from the same shell.

bash# MYSQLCPPCONN_TRACE_ENABLED=1 ./mysqlconnectorc++client#    >MySQL_Connection::init#    |  INF: hostName=tcp://:#    |  INF: user=root#    |  INF: port=0#    |  INF: schema=#    |  INF: socket=#    |  >MySQL_Connection::setAutoCommit#    |  MySQL_Connection::setTransactionIsolation#    |  

Controlling the debug traces with the environment variable is simpleand does not require any code instrumentation in the application. Itgenerates the traces for all the driver specific function calls fromthe application. But if you want per connection debug traces orfine-grained control over the traces to be generated, considerinstrumenting your code with the Connection::setClientOptionsmethod. For example, if you want to see what is happening inside thedriver when a driver supported function is called from the application,call setClientOptions() twice from your application --once to enable the client tracing right before calling the driverspecific function you are interested in, and the second call is todisable the client tracing right after calling the function.

The following code fragment demonstrates how to generate connector's internal debug trace for the prepareStatement() function call using Connection::setClientOptions().

Driver *driver;Connection *con;PreparedStatement *prep_stmt;string url, user, password;int on_off = 0;try {driver = get_driver_instance();con = driver -> connect(url, user, password);/* enable client tracing */on_off = 1;con -> setClientOption ("clientTrace", &on_off);prep_stmt = con -> prepareStatement ("INSERT INTO City (CityName) VALUES (?)");/* disable client tracing */on_off = 0;con -> setClientOption ("clientTrace", &on_off);} catch (SQLException &e) {..}

The call to setClientOption method shown in the above code causes the debug trace to be written to the stdout. It is not required to set the environment variable MYSQLCPPCONN_TRACE_ENABLED in this case.

Be aware that trace enabled versions may cause higher CPU usage evenif the overall run time of your application is not impactedsignificantly. There will be some additional I/O overhead if the debugtrace has to be written to a trace file.

For More Information

  • MySQL Server
    • MySQL Reference Manual
  • MySQL Connector/C++
    • MySQL Connector/C++ Project Page
    • MySQL Connector/C++ Reference Manual
    • MySQL Connector/C++ : Installing from Source
    • Notes on using the MySQL Connector/C++ API
    • MySQL Connector/C++ : Filtering Debug Output
    • MySQL Connector/C++ Known Bugs and Issues. Report new bugs at bugs.mysql.com
    • MySQL Connector/C++ Change History
    • MySQL Connector/C++ Feature requests
    • MySQL Connector/C++ mailing list
    • MySQL Connector/C++ discussion forum
  • MySQL Connector/C
    • MySQL Connector/C 6.0 download page
    • MySQL Connector/C Reference Manual
  • NetBeans IDE
    • MySQL Connector/C++: How to Build a Client on Linux using NetBeans 6.5 (for Dummies)
    • Tutorials: Developing C and C++ Applications using NetBeans IDE
  • Visual Studio IDE
    • MySQL Connector/C++: Guide on building a Windows Client using Visual Studio (for Dummies)
  • JDBC
    • JDBC 4.0 API Specification
    • JDBC Basics
  • Weblogs
    • Ulf Wendel's weblog
    • Andrey Hristov's weblog
    • Lance Andersen's weblog

Acknowledgments

The author would like to acknowledge Andrey Hristov, Ulf Wendel,Lance Andersen and Edwin DeSouza of Sun | MySQL AB, for the extensivefeedback on this article.

Bact to the top

  • Developer Zone
  • Documentation
  • Librarian
  • Developer Articles
  • News & Events
  • Forums
  • Bugs
  • Forge
  • Planet MySQL
  • Labs
  • Downloads
  • MySQL Community Server
  • MySQL Proxy
  • MySQL Cluster
  • MySQL Workbench
  • Connectors
  • Archives
  • Snapshots
  • Mirrors
  • Documentation
  • MySQL Reference Manuals
  • MySQL Workbench
  • Expert Guides
  • Topic Guides
  • MySQL Cluster
  • Other Documents
  • MySQL University
  • About
  • Archives
  • Support
  • MySQL Meetups
  • Guilds
  • Lists
  • Forums
  • Other
  • Legal
  • Privacy Policy
  • Contact Us
  • Site Map
  © 2010, Oracle Corporation and/or its affiliates