/*
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 > ? argv[] : DEFAULT_URI);
const string user(argc >= ? argv[] : EXAMPLE_USER);
const string pass(argc >= ? argv[] : EXAMPLE_PASS);
const string database(argc >= ? argv[] : 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 << "\t... running 'SELECT 'Welcome to Connector/C++' AS _message'"
<< endl;
while (res->next())
{
cout << "\t... MySQL replies: " << res->getString("_message") << endl;
cout << "\t... say it again, MySQL" << endl;
cout << "\t....MySQL replies: " << res->getString() << 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;
}

最新文章

  1. sql case when then else end
  2. html中代码高亮显示
  3. XML的介绍
  4. phpDoc 注释案例说明
  5. java 堆与栈的区别
  6. IP地址基础和子网规划之其一
  7. opencv如何用模板匹配寻找目标
  8. JS中判定问题
  9. Next Greater Element I
  10. python并发编程基础之守护进程、队列、锁
  11. 【数学建模】day02-整数规划
  12. .NET基础之this关键字
  13. matplotlib 与 seaborn 中出现中文乱码的解决方法
  14. Zabbix poller processes more than 75% busy
  15. 『MXNet』第七弹_多GPU并行程序设计
  16. Django框架之下载以及基本命令
  17. 20145227鄢曼君《网络对抗》Web基础
  18. sockets+proxychains代理,使内网服务器可以访问外网
  19. loadrunner汉化【运行时设置】菜单选项截图
  20. sql 如何更改表的字段属性

热门文章

  1. 深入理解Java虚拟机-第1章-走进Java-读书笔记
  2. MySQL学习(一)日志与索引 --- 2019年1月
  3. javascript放大镜效果
  4. 文本分布式表示(一):word2vec理论
  5. 目标检测 非极大值抑制(Non-Maximum Suppression,NMS)
  6. 简述java接口和C++虚类的相同和不同之处
  7. Java内存模型JMM 高并发原子性可见性有序性简介 多线程中篇(十)
  8. CSS3文字与字体 text-overflow 与 word-wrap
  9. 3.SDL落地方案
  10. CODING 如何使用 CODING 研发管理系统来敏捷开发