Lance Andersen
MySQL Connector/C++ 1.0.1 Alpha Released offering a JDBC API for MySQL C++ developers
The MySQL Connector/C++ Connector project has successfully reached its first major milestone by releasing its Alpha version. The Connector provides a C++ implementation modeled after JDBC. This allows developers who have worked with JDBC to quickly take advantage of this Connector minimizing the learning curve. The current release provides support for many of the methods found in the following JDBC interfaces:
- java.sql.Connection
- java.sql.DatabaseMetaData
- java.sql.Driver
- java.sql.PreparedStatement
- java.sql.ResultSet
- java.sql.ResultsetMetaData
- java.sql.Savepoint
- java.sql.Statement
Here is an example which connects to a MySQL Server, creates a table and then inserts a row into the table
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306", "user", "password");
stmt = con->createStatement();
stmt->executeUpdate("DROP TABLE IF EXISTS CONTACTS");
stmt->executeUpdate("CREATE TABLE CONTACTS(ID INT NOT NULL AUTO_INCREMENT, FNAME VARCHAR(25), LNAME VARCHAR(25), PHONE VARCHAR(12))");
stmt->executeUpdate("INSERT INTO CONTACTS( FNAME, LNAME, PHONE) VALUES ('Bruce', 'Wayne', '555 555-5555')");
delete stmt;
delete con;
This example uses a PreparedStatement to insert a couple of rows
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::PreparedStatement *pstmt
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306", "user", "password");
pstmt = con->prepareStatement("INSERT INTO CONTACTS( FNAME, LNAME, PHONE) VALUES ( ?, ?, ?)");
pstmt->setString(1, "Dick");
pstmt->setString(2, "Grayson");
pstmt->setString(3, "222 222-2222");
pstmt->executeUpdate();
pstmt->setString(1, "Clark");
pstmt->setString(2, "Kent");
pstmt->setString(3, "333 333-3333");
pstmt->executeUpdate();
delete pstmt;
delete con;
This example uses a uses a PreparedSatement to execute a query and then process a ResultSet. Notice that you can reference the columns via an ordinal or a column name
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::PreparedStatement *pstmt;
sql::ResultSet *rs;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306", "user", "password");
pstmt = con->prepareStatement("SELECT * FROM CONTACTS WHERE ID= ?");
pstmt->setInt(1, 2);
rs= pstmt->executeQuery();
while (rs->next()) {
cout << "ID = " << rs->getInt(0);
cout <<", First Name = '" <<rs->getString("FNAME") << "'";
cout <<", Last Name = '" <<rs->getString("LNAME") << "'";
cout <<", Phone = '" <<rs->getString(3) << "'" << endl;
}
delete rs;
delete pstmt;
delete con;
As you can see from the above examples, the MySQL Connector/C++ connector provides a simple API modeled on JDBC for accessing your MySQL data.
The MySQL Connector/C++ connector is being used today by MySQL Workbench and the MySQL/OpenOffice.org Connector. We hope you give it a go and let us know what you think.
For more information on MySQL Connector/C++, please refer to the following sites:
- MySQL Connector/C++ User documentation
- MySQL Connector/C++ Community Forum
- MySQL Connector/C++ Mailing list
- MySQL Connector/C++ Project Page
- MySQL Connector/C++ Source Download Page
Technorati Tags: databases, JDBC, mysql
Posted at 06:40AM Dec 04, 2008 by lancea in Glassfish | Comments[1]


Can you help me?
I want to use MySQL Connector/C++ on C++ Builder.
I was compile MySQL Connector/C++ but I meet some errors.
How to solve this problem?
F:\Dev\MySQL\MySQL_Conn\v1_0_1_bcc>make
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
[ 2%] Building CXX object driver/CMakeFiles/mysqlcppconn.dir/mysql_constructed_
resultset.obj
Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
F:\Dev\MySQL\MySQL_Conn\v1_0_1_bcc\driver\mysql_constructed_resultset.cpp:
Error E2167 C:\Program Files\Borland\BDS\4.0\Include\math.h 390: 'std::abs(long)
' was previously declared with the language 'C++'
Error E2141 F:\Dev\MySQL\mysql-5.1.30-win32\include\my_dbug.h 48: Declaration sy
ntax error
*** 2 errors in Compile ***
** error 1 ** deleting driver\CMakeFiles\mysqlcppconn.dir\mysql_constructed_resu
ltset.obj
** error 1 ** deleting driver\CMakeFiles\mysqlcppconn.dir\all
** error 1 ** deleting all
F:\Dev\MySQL\MySQL_Conn\v1_0_1_bcc>
Posted by ASURADA on December 04, 2008 at 11:18 PM EST #