#include #include //for the i/o manipulator setw (for "set width") #include #include int main() { //Database credentials. //JDBC is "Java Database Connectivity". //3306 is the default port number for MySQL. const std::string url {"jdbc:mariadb://localhost:3306/demo2"}; const std::string user {""}; //your username const std::string password {""}; //your password try { //Get the MariaDB driver. const sql::Driver const* driver {sql::mariadb::get_driver_instance()}; //Configure connection properties. const sql::Properties properties { {"user", user}, {"password", password} }; //Use the driver to establish a connection. const std::unique_ptr conn {driver->connect(url, properties)}; //Use the connection to create a statement. const std::unique_ptr stmt {conn->createStatement()}; //Use the statement to execute a query. const std::string query {"SELECT * FROM addrbook"}; const std::unique_ptr res {stmt->executeQuery(query)}; //Display the result set. std::cout << "lastname firstname zip\n" << "-------- --------- ---\n"; //column headings while (res->next()) { std::cout << std::left << std::setw(8) << res->getString("lastname") << " " << std::left << std::setw(11) << res->getString("firstname") << " " << std::left << std::setw(5) << res->getInt("zip") << "\n"; } } catch (const sql::SQLException& e) { std::cerr << "Error: " << e.what() << "\n"; return EXIT_FAILURE; } return EXIT_SUCCESS; //Arrive here if no exception was thrown. }