I'm having trouble with the simple task of creating a connection to a MySQL database in C++. No errors are printed except for the program return value, which is -1.
I'm using Eclipse Oxygen as the IDE, and the OS is Ubuntu 16.04. The MySQL connector version is 1.1.9., and it's the generic Linux version (glibc 2.12).
The connector files reside in my home folder. I've added the directory path of the "include" folder to Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes. I've also added the "lib" path and "mysqlcppconn" library name to Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries and Library search path, respectively. Finally, I've added the "lib" path into the LD_LIBRARY_PATH environment variable in Properties -> Run/Debug Settings -> (Launch configuration) -> Environment.
DBManager.cpp:
DBManager::DBManager() {
try {
std::cout << "Getting driver instance" << std:endl;
driver = sql::mysql::get_mysql_driver_instance();
std::cout << "Creating connection" << std::endl;
connection = driver->connect("tcp://127.0.0.1:3306", "username", "password");
std::cout << "Connected!" << std::endl; // NOT REACHING THIS LINE
} catch(const SQLException& ex) {
// these lines are not printed
std::cout << "Exception!" << std::endl;
std::cout << ex.what() << std::endl;
exit(1);
}
}
DBManager::~DBManager() {
std::cout << "Deleting connection" << std::endl;
delete connection;
}
DBManager.h
#ifndef DBMANAGER_H_
#define DBMANAGER_H_
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>
class DBManager {
public:
DBManager();
virtual ~DBManager();
private:
sql::mysql::MySQL_Driver* driver;
sql::Connection* connection;
};
#endif
main.cpp
#include <iostream>
#include "DBManager.h"
int main(int argc, char* argv[]) {
std::cout << "Starting" << std::endl;
DBManager* dbManager = new DBManager();
// ... program logic not reached
delete dbManager; // not reached
return 0;
}