7.1 Connector/C++ Connecting to MySQL

To establish a connection to MySQL Server, retrieve an instance of sql::Connection from a sql::mysql::MySQL_Driver object. A sql::mysql::MySQL_Driver object is returned by sql::mysql::MySQL_Driver::get_mysql_driver_instance().

sql::mysql::MySQL_Driver *driver;
sql::Connection *con;

driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
con = driver->connect("tcp://", "user", "password");

delete con;

Make sure that you free, con, the sql::Connection object as soon as you do not need it any more. But do not explicitly free driver, the connector object! The connector will take care of freeing that.

As of Connector/C++, these methods can be used to check the connection state or reconnect:

  • sql::Connection::isValid() checks whether the connection is alive

  • sql::Connection::reconnect() reconnects if the connection has gone down

Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb
EPUB - 0.7Mb
HTML Download (TGZ) - 0.6Mb
HTML Download (Zip) - 0.7Mb
User Comments
Sign Up Login You must be logged in to post a comment.