0
mysql -uroot -p123456

i can connect to mysql with a terminal. but when i use the mysql c++ connector with jdbc, it fails. The exception says "unable to connect to localhost".

            driver = sql::mysql::get_driver_instance();
            try {
                // con = driver->connect(connection, username, password);
                 con = driver->connect("tcp://127.0.0.1:3306", "root", "123456");
            }
            catch (sql::SQLException e) {
                std::cout << e.what();
            }

            con->setSchema("mydb");

I am using mysql cpp connector8 jdbc api. but i always get the error unable connect to localhost. any help?

user3135996
  • 176
  • 1
  • 1
  • 10
  • Is mysql running on localhost? If so, is it running with a tcp port or a socket? There's not much information in your post regarding how your mysql is setup – ewokx Apr 18 '23 at 06:20
  • i run mysql -uroot -p123456 on a terminal, it works – user3135996 Apr 18 '23 at 06:26
  • 1
    Localhost is 127.0.0.1 basically, so what is the use of ":3306" for you ? Because adding this specifies the port "3306" instead of "123456" – Emerald Cottet Apr 18 '23 at 06:29
  • This post may also help : https://stackoverflow.com/questions/25777943/failed-to-connect-to-mysql-at-127-0-0-13306-with-user-root-access-denied-for-us?rq=2 – Emerald Cottet Apr 18 '23 at 06:39

1 Answers1

0

enter image description here

i have solved the problem. when develop on windows, we must download the debug version, not the release version.

user3135996
  • 176
  • 1
  • 1
  • 10