Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <mysql-cppconn/jdbc/mysql_connection.h>
  4. #include <mysql-cppconn/jdbc/cppconn/driver.h>
  5. #include <mysql-cppconn/jdbc/cppconn/exception.h>
  6. #include <mysql-cppconn/jdbc/cppconn/resultset.h>
  7. #include <mysql-cppconn/jdbc/cppconn/statement.h>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13. // clear the console screen
  14. system("clear");
  15.  
  16. cout << endl;
  17. cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
  18.  
  19. try{
  20. sql::Driver *driver;
  21. sql::Connection *conn;
  22. sql::Statement *stmt;
  23. sql::ResultSet *res;
  24.  
  25. // create connection
  26. driver = get_driver_instance();
  27. conn = driver->connect("tcp://127.0.0.1:3306", "root", "test");
  28. // connect to MySQL datbase
  29. conn->setSchema("R42");
  30.  
  31. stmt = conn->createStatement();
  32. res = stmt->executeQuery("SELECT 'Hello World' AS _message");
  33. while(res->next()) {
  34. cout << "\t... MySQL replies: ";
  35. // access column data by alias or column name
  36. cout << res->getString("_message") << endl;
  37. cout << "\t... MySQL says it again: ";
  38. // access column data by numeric offset, 1 is the first column
  39. cout << res->getString(1) << endl;
  40. }
  41. delete res;
  42. delete stmt;
  43. delete conn;
  44. } catch (sql::SQLException &e) {
  45. cout << "# ERR: SQLException in " << __FILE__;
  46. cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  47. cout << "# ERR: " << e.what();
  48. cout << " MySQL Error Code; " << e.getErrorCode();
  49. cout << ", SQLState: " << e.getSQLState() << " )" << endl;
  50. }
  51. cout << endl;
  52.  
  53. return EXIT_SUCCESS;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement