Advertisement
ACI-XCIX_0001

Untitled

Dec 30th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <mysql_driver.h>
  3. #include <mysql_connection.h>
  4. #include <cppconn/exception.h>
  5.  
  6. int main() {
  7.     try {
  8.         std::cout << "Initializing MySQL driver..." << std::endl;
  9.         sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance();
  10.         std::cout << "MySQL driver initialized successfully!" << std::endl;
  11.  
  12.         std::string connection_string = "tcp://127.0.0.1:3306";
  13.         std::cout << "Connection string: " << connection_string << std::endl;
  14.  
  15.         std::cout << "Connecting to the database at '" << connection_string << "' with user 'root'..." << std::endl;
  16.         std::unique_ptr<sql::Connection> con(driver->connect(connection_string, "root", ""));
  17.         std::cout << "Successfully connected to the database!" << std::endl;
  18.  
  19.     }
  20.     catch (sql::SQLException& e) {
  21.         std::cerr << "SQLException: " << e.what() << std::endl;
  22.         std::cerr << "SQLState: " << e.getSQLState() << std::endl;
  23.         std::cerr << "Error Code: " << e.getErrorCode() << std::endl;
  24.     }
  25.     catch (std::exception& e) {
  26.         std::cerr << "Exception: " << e.what() << std::endl;
  27.     }
  28.     catch (...) {
  29.         std::cerr << "Unknown exception occurred!" << std::endl;
  30.     }
  31.  
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement