Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

C++ [SOLVED] Trying to connect to MYSQL via c++

views
     
TSkirakosmos
post Jul 30 2025, 02:27 PM, updated 4 months ago

On my way
****
Junior Member
506 posts

Joined: Jul 2009
I'm learning a bit of c++ a long ago so I try to connect with MYSQL
CODE
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

int main()
{
   sql::mysql::MySQL_Driver* driver;
   sql::Connection* conn;
   sql::PreparedStatement* pstm;

   std::string nama;
   int a, umur;

   std::cout << "Masukkan jumlah data: ";
   std::cin >> a;

   try {
       driver = sql::mysql::get_mysql_driver_instance();
       conn = driver->connect("tcp://127.0.0.1:3306", "root", "password"); // adjust credential after test
       conn->setSchema("test1"); // databaseName
       //}

       for (int i = 0; i < a; ++i) {
           std::cout << "Masukkan nama perserta: ";
           std::cin >> nama;
           std::cout << " Masukkan umur perserta: ";
           std::cin >> umur;

           pstm = conn->prepareStatement("INSERT INTO userData(nama, umur) VALUES (? , ? )");
           pstm->setString(1, nama);
           pstm->setInt(2, umur);
           pstm->execute();

           std::cout << " Data " << i + 1 << " dimasukkan.\n";
           delete pstm;
       }
       delete conn;
       std::cout << "Hello World! Data sudah disimpan.\n";
       return 0;
   }
   catch (sql::SQLException& e) {
       std::cerr << "SQL Error: " << e.what()
           << "\nMySQL Error Code: " << e.getErrorCode()
           << "\nSQLState: " << e.getSQLState()
           << std::endl;
   }
}

I try to connect but it throws me an exception that it Can't connect with MYSQL. Basically the error I got is:
» Click to show Spoiler - click again to hide... «

but on he console it said unable to connect (gibberish) 3306

Firewall already allowed port 3306 because it connect in workbench... Can someone help me in this? Or because of my latop spec?

SOLVED> Thank fr anyone who help

This post has been edited by kirakosmos: Aug 27 2025, 11:08 AM
flashang
post Jul 30 2025, 08:09 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


May not solve your problem :

1. your error message does not match try catch message.
2. have you tried use mysql command line ?
3. have you tried other language ?

Note : did not try with c++

hmm.gif


TSkirakosmos
post Jul 31 2025, 09:25 AM

On my way
****
Junior Member
506 posts

Joined: Jul 2009
QUOTE(flashang @ Jul 30 2025, 08:09 PM)
May not solve your problem :

1. your error message does not match try catch message.
2. have you tried use mysql command line ?
3. have you tried other language ?

Note : did not try with c++

hmm.gif
*
I try my best to answer...

1. there maybe an exception catch by VS.
2. I use them and it work perfectly fine. can connect everything
3. I try to use with apache (XAMPP) and when start the MYSQL, minute/second after it suddenly stop.

basically, database only work when using command line

This post has been edited by kirakosmos: Jul 31 2025, 09:28 AM
flashang
post Jul 31 2025, 09:00 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


QUOTE(kirakosmos @ Jul 31 2025, 09:25 AM)
I try my best to answer...

1. there maybe an exception catch by VS.
2. I use them and it work perfectly fine. can connect everything
3. I try to use with apache (XAMPP) and when start the MYSQL, minute/second after it suddenly stop.

basically, database only work when using command line
*
try other programming language, e.g. python, node.js, php.
you may need to get some better error message to identify the error.

hmm.gif


TSkirakosmos
post Aug 7 2025, 10:29 AM

On my way
****
Junior Member
506 posts

Joined: Jul 2009
I change exception catch a bit and this error prompted when building
CODE
Variable 'sql::Variant::variant' is uninitialized. Always initialize a member variable (type.6).

flashang
post Aug 7 2025, 08:38 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


QUOTE(kirakosmos @ Aug 7 2025, 10:29 AM)
I change exception catch a bit and this error prompted when building
CODE
Variable 'sql::Variant::variant' is uninitialized. Always initialize a member variable (type.6).

*
From MySQL website Example :

MySQL Connector/C++: Connector/C++ classic JDBC API Example
https://dev.mysql.com/doc/dev/connector-cpp...bc_example.html

CODE

   sql::Driver * driver = sql::mysql::get_driver_instance();

   /* Using the Driver to create a connection */

   cout << "Creating session on " << url << " ..."
        << endl << endl;

   std::unique_ptr< sql::Connection >
     con{driver->connect(url, user, pass)};


smile.gif



This post has been edited by flashang: Aug 7 2025, 08:38 PM
TSkirakosmos
post Aug 25 2025, 12:24 PM

On my way
****
Junior Member
506 posts

Joined: Jul 2009
Sorry for not updating for a long time due to I was admitted to a hospital. Anyway I play around the code and I find that bad_alloc exception being thrown
CODE

Memory allocation failed: bad allocation

Is there any sifu can help?
MatQuasar
post Oct 16 2025, 06:47 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
Nice attempt by TS. rclxms.gif

This post has been edited by MatQuasar: Oct 16 2025, 07:33 PM

 

Change to:
| Lo-Fi Version
0.0172sec    0.29    5 queries    GZIP Disabled
Time is now: 21st December 2025 - 07:26 PM