• mysql jdbc 官方编程示例


    /*
       Basic example of an application using JDBC API of Connector/C++
    */
    /* Standard C++ includes */
    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <stdexcept>
    /*
      Note: Boost must be in the include path to build code which uses the
      JDBC API.
    */
    #include <boost/scoped_ptr.hpp>
    #include <jdbc/mysql_connection.h>
    #include <jdbc/mysql_driver.h>
    #include <jdbc/cppconn/resultset.h>
    #include <jdbc/cppconn/statement.h>
    #define DEFAULT_URI "tcp://127.0.0.1"
    #define EXAMPLE_USER "root"
    #define EXAMPLE_PASS ""
    #define EXAMPLE_DB "test"
    using namespace std;
    /*
      Usage example for Driver, Connection, (simple) Statement, ResultSet
    */
    int main(int argc, const char **argv)
    {
      const char   *url = (argc > 1 ? argv[1] : DEFAULT_URI);
      const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
      const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
      const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);
      cout << endl;
      cout << "Connector/C++ standalone program example..." << endl;
      cout << endl;
      try {
        sql::Driver * driver = sql::mysql::get_driver_instance();
        /* Using the Driver to create a connection */
        cout << "Creating session on " << url << " ..."
             << endl << endl;
        boost::scoped_ptr< sql::Connection >
          con(driver->connect(url, user, pass));
        con->setSchema(database);
        boost::scoped_ptr< sql::Statement > stmt(con->createStatement());
        boost::scoped_ptr< sql::ResultSet >
          res(stmt->executeQuery("SELECT 'Welcome to Connector/C++' AS _message"));
        cout << "	... running 'SELECT 'Welcome to Connector/C++' AS _message'"
             << endl;
        while (res->next())
        {
          cout << "	... MySQL replies: " << res->getString("_message") << endl;
          cout << "	... say it again, MySQL" << endl;
          cout << "	....MySQL replies: " << res->getString(1) << endl;
        }
      }
      catch (sql::SQLException &e)
      {
        /*
          The JDBC API throws three different exceptions:
        - sql::MethodNotImplementedException (derived from sql::SQLException)
        - sql::InvalidArgumentException (derived from sql::SQLException)
        - sql::SQLException (derived from std::runtime_error)
        */
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << "EXAMPLE_FUNCTION" << ") on line " << __LINE__ << endl;
        /* Use what() (derived from std::runtime_error) to fetch the error message */
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        return EXIT_FAILURE;
      }
      cout << endl;
      cout << "... find more at http://www.mysql.com" << endl;
      cout << endl;
      return EXIT_SUCCESS;
    }
  • 相关阅读:
    玩转java多线程(wait和notifyAll的正确使用姿势)
    shell脚本编写之Hello World
    面试题录:数据库篇
    面试题录:笔试题篇
    浅谈String、StringBuffer与StringBuilder
    Java攻城狮面试题录:笔试篇(1)
    Android程序中,内嵌ELF可执行文件-- Android开发C语言混合编程总结
    TensorFlow从1到2(十五)(完结)在浏览器做机器学习
    TensorFlow从1到2(十四)评估器的使用和泰坦尼克号乘客分析
    TensorFlow从1到2(十二)生成对抗网络GAN和图片自动生成
  • 原文地址:https://www.cnblogs.com/qianlicao/p/10535514.html
Copyright © 2020-2023  润新知