Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #include "mysql_connection.h"
- #include <cppconn/driver.h>
- #include <cppconn/exception.h>
- #include <cppconn/resultset.h>
- #include <cppconn/statement.h>
- using namespace std;
- int main(void)
- {
- float username, password;
- cout << endl;
- cout << "Running 'SELECT 'Hello World!' »
- AS _message'..." << endl;
- cin >> "Username? "username;
- cin >> "Password: "password;
- try {
- sql::Driver *driver;
- sql::Connection *con;
- sql::Statement *stmt;
- sql::ResultSet *res;
- /* Create a connection */
- driver = get_driver_instance();
- con = driver->connect("tcp://127.0.0.1:3306", username, password);
- /* Connect to the MySQL test database */
- con->setSchema("test");
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
- while (res->next()) {
- cout << "\t... MySQL replies: ";
- /* Access column data by alias or column name */
- cout << res->getString("_message") << endl;
- cout << "\t... MySQL says it again: ";
- /* Access column data by numeric offset, 1 is the first column */
- cout << res->getString(1) << endl;
- }
- delete res;
- delete stmt;
- delete con;
- } catch (sql::SQLException &e) {
- cout << "# ERR: SQLException in " << __FILE__;
- cout << "(" << __FUNCTION__ << ") on line " »
- << __LINE__ << endl;
- cout << "# ERR: " << e.what();
- cout << " (MySQL error code: " << e.getErrorCode();
- cout << ", SQLState: " << e.getSQLState() << " )" << endl;
- }
- cout << endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement