Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <stdexcept>
  5.    
  6. #include "mysql_connection.h"
  7.    
  8. #include <cppconn/driver.h>
  9. #include <cppconn/exception.h>
  10. #include <cppconn/resultset.h>
  11. #include <cppconn/statement.h>
  12. #include <cppconn/prepared_statement.h>
  13.    
  14. #define EXAMPLE_HOST "localhost"
  15. #define EXAMPLE_USER "root"
  16. #define EXAMPLE_USER "Quadro88"
  17. #define EXAMPLE_DB "db_ved_wine"
  18.  
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23.     try {
  24.    
  25.     /* INSERT TUTORIAL CODE HERE! */
  26.    
  27.     sql::mysql::MySQL_Driver *driver;
  28.     sql::Connection *con;
  29.     sql::Statement *stmt;
  30.    
  31.     driver = sql::mysql::get_mysql_driver_instance();
  32.     con = driver->connect("tcp://"EXAMPLE_HOST":3306", EXAMPLE_USER, EXAMPLE_USER);
  33.  
  34.     stmt = con->createStatement();
  35.     stmt->execute("USE " EXAMPLE_DB);
  36.     stmt->execute("SELECT * FROM products");
  37.    
  38.     delete stmt;
  39.     delete con;
  40.    
  41.     } catch (sql::SQLException &e) {
  42.         /*
  43.           The MySQL Connector/C++ throws three different exceptions:
  44.    
  45.           - sql::MethodNotImplementedException (derived from sql::SQLException)
  46.           - sql::InvalidArgumentException (derived from sql::SQLException)
  47.           - sql::SQLException (derived from std::runtime_error)
  48.         */
  49.         cout << "# ERR: SQLException in " << __FILE__;
  50.         cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  51.         /* Use what() (derived from std::runtime_error) to fetch the error message */
  52.         cout << "# ERR: " << e.what();
  53.         cout << " (MySQL error code: " << e.getErrorCode();
  54.         cout << ", SQLState: " << e.getSQLState() << " )" << endl;
  55.    
  56.         return EXIT_FAILURE;
  57.     }
  58.    
  59.     cout << "Done." << endl;
  60.     return EXIT_SUCCESS;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement