DBA > Articles

Developing Database Applications Using MySQL Connector/C++

By: by Brad McGehee
To read more DBA articles, visit http://dba.fyicenter.com/article/

The MySQL Connector/C++ provides an object-oriented application programming interface (API) and a database driver for connecting C++ applications to the MySQL Server. Connector/C++ implemented a significant percentage of the industry standard JDBC 4.0 specification in C++ world. This technical article shows the essential steps to build and install MySQL Connector/C++ driver, with simple examples to connect, insert, and retrieve data from a MySQL database. Application developers who are new to MySQL Connector/C++ but not to C++ programming and MySQL database, are the target audience.

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

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

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

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 an object-oriented application programming interface (API) and a database driver for connecting C++ applications to the MySQL Server.

The development of Connector/C++ took a different approach compared to the existing drivers for C++ by implementing the JDBC API in C++ world. In other words, Connector/C++ driver's interface is mostly based on Java programming language's JDBC API. Java Database Connectivity (JDBC) API is the industry standard for connectivity between the Java programming 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 find this useful, and as a result, it could improve application development time.

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 binary form for Solaris, Linux, Windows, FreeBSD, Mac OS X, HP-UX and AIX platforms. MSI installer and a binary zip file without the installer is available for Windows, where as the binary package is available as compressed GNU TAR archive (tar.gz) for the rest of the platforms. You can download the pre-compiled binary from the Connector/C++ download page.

Installation from the binary package is very straight forward on Windows and other platforms - simply unpacking the archive in a desired location installs the Connector/C++ driver. Both statically linked and the dynamically linked Connector/C++ driver can be found in lib directory under the driver installation directory. If you plan to use the dynamically linked version of MySQL Connector/C++, ensure that the runtime linker can find the MySQL Client Library. Consult your operating system documentation for the steps to modify and expand the search path for libraries. In case you cannot modify the library search path, copy your application, the MySQL Connector/C++ driver and the MySQL Client Library into the same directory. This approach may work on most of the platforms as they search the originating directory by default, 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 Client Library, dynamically linked C++ applications that use Connector/C++ will require the MySQL client programming support installed along with the connector driver, on the machines where the application is supposed to run. Static linking with the MySQL Client Library is one of the options 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 Pack lets C/C++ developers use their specified set of compilers and tools in conjunction with NetBeans IDE to build native applications on Solaris, Linux, Windows and Mac OS X. The C/C++ development pack makes the editor language-aware for C/C++ and provides project templates, a dynamic class browser, Makefile support &m debugger functionality. It is possible to extend C/C++ development pack base functionality with new 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 client applications based on Connector/C++. In addition to the above tutorial, Installing and Configuring C/C++ Support tutorial on NetBeans.org web site will help you with the installation and 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 -p
Enter password: admin
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.24-rc-standard Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.



mysql> USE test;
Database changed

mysql> 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: City
Create Table: CREATE TABLE `City` (
`CityName` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ascii
1 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.

Full article...


Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/