Advertisement
Guest User

Untitled

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