MySQL Connector error: Unable to connect to localhost

72 views Asked by At

I am trying to read data from a table I created in the schema. But when connecting, it outputs an error:Unable to connect to localhost I use the connector 8.1 debug version, I specified the path to the include and lib folders in Visual Studio. When connecting to the mysql server via workbench, everything works.

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
using namespace std;

//for demonstration only. never save your password in the code!
const string server = "tcp://127.0.0.1:3306";
const string username = "root";
const string password = "";



int main()
{
    sql::Driver* driver;
    sql::Connection* con;
    sql::PreparedStatement* pstmt;
    sql::ResultSet* result;

    try
    {
        driver = get_driver_instance();
        //for demonstration only. never save password in the code!
        con = driver->connect(server, username, password);
    }
    catch (sql::SQLException e)
    {
        cout << "Could not connect to server. Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    con->setSchema("mySchema");

    //select  
    pstmt = con->prepareStatement("SELECT * FROM players;");
    result = pstmt->executeQuery();

    while (result->next())
        printf("Reading from table=(%d, %s, %d)\n", result->getInt(1), result->getString(2).c_str(), result->getInt(3));

    delete result;
    delete pstmt;
    delete con;
    system("pause");
    return 0;
}

I tried to install other versions of connector, but there was no effect. Changed ip 127.0.0.1 to localhost also nothing has changed

0

There are 0 answers