Skip to main content

MySQL Connector/C++ 1.0.1 Alpha Released offering a JDBC API for MySQL C++ developers

Posted by lancea on December 4, 2008 at 3:38 AM PST

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

Examples of using MySQL Connector/C++

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("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->setString(1, "Clark");
        pstmt->setString(2, "Kent");
        pstmt->setString(3, "333 333-3333");
        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/ 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:

Technorati Tags: , ,

Related Topics >>