/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#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)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
AS _message'..." << endl;
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", "root", "root");
/* 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;
}
MySQL Connector/C++ 例子(一)
原创wx604f04a92c6fd 博主文章分类:数据库MySQL ©著作权
文章标签 MySQL ConnectorC++ mysql c++ sql #include 文章分类 MySQL 数据库
©著作权归作者所有:来自51CTO博客作者wx604f04a92c6fd的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:go语言文件读写
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
数据库连接池c++ (msyql) mysql connector c++ 的使用
Mysql Connector/C++ 使用介绍一、下载mysql connector c++和 boost库wget https://dev.mysql.com/get/Downloads/Connector-C++/mysql-connec
mysql 数据库连接池 mysql connector c++ msyql 连接池 #include -
Mysql Connector(C++)的数据库连接池的实现
基于MysqlConnector/C++的数据库连接池的实现1.连接池的介绍:1.1应用背景:一般的应用程序都会访问种数据库连接,必然会影响到系
mysql 数据库连接池 数据库连接池 连接池 数据库 mysql c++ -
【C/C++】C/C++编程——C++ 开发环境搭建
对于刚开始学习C++的朋友,我比较建议使用Visual Studio IDE。Visual Studio 基本不需要怎么配置,下载之后即可直接写代码
c语言 c++ 开发语言 集成开发环境 Visual -
C++ | C++模板
C++ | C++模板模板是泛型编程的基础,泛型编程即以一种独立于任何特定类型的方式编写代码。模板是创建泛型
c++ Stack #include 数据类型