Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mysql_driver.h>
- #include <mysql_connection.h>
- #include <cppconn/exception.h>
- int main() {
- try {
- std::cout << "Initializing MySQL driver..." << std::endl;
- sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance();
- std::cout << "MySQL driver initialized successfully!" << std::endl;
- std::string connection_string = "tcp://127.0.0.1:3306";
- std::cout << "Connection string: " << connection_string << std::endl;
- std::cout << "Connecting to the database at '" << connection_string << "' with user 'root'..." << std::endl;
- std::unique_ptr<sql::Connection> con(driver->connect(connection_string, "root", ""));
- std::cout << "Successfully connected to the database!" << std::endl;
- }
- catch (sql::SQLException& e) {
- std::cerr << "SQLException: " << e.what() << std::endl;
- std::cerr << "SQLState: " << e.getSQLState() << std::endl;
- std::cerr << "Error Code: " << e.getErrorCode() << std::endl;
- }
- catch (std::exception& e) {
- std::cerr << "Exception: " << e.what() << std::endl;
- }
- catch (...) {
- std::cerr << "Unknown exception occurred!" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement