使用注意事项

 

 1 # wget https://cdn.mysql.com//Downloads/Connector-C++/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit.tar.gz
 2 
 3 如果出错, 试试安装
 4 # yum install -y boost
 5 # yum install -y boost-devel
 6 
 7 
 8 https://dev.mysql.com/downloads/connector/cpp/
 9 
10 官网提供的 Windows 版本 mysql-connector-c++-8.0.13-win 在Debug下运行Crash, 更改为Release。
11 调试: VS工程属性--链接器--调试--生成调试信息
# g++ main.cpp JdbcHelper.cpp -std=c++11 -I/usr/local/mysql_cpp/include/jdbc -L/usr/local/mysql_cpp/lib64 -lmysqlcppconn -lssl -lcrypto 

 

JdbcHelper.h

 

#pragma once
#include <iostream>
#include <string>
#include <functional>

//#include <mysql_driver.h>
//#include <mysql_connection.h>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>


class JdbcHelper
{
public:
    /** host: tcp://192.168.6.80:23306 自动重连*/
    JdbcHelper(const std::string& host, const std::string& username, const std::string& passwd, const std::string& database);
    virtual ~JdbcHelper();

    /** 连接 */
    int jdbc_connect(bool enableSSL);
    /** 断开连接 */
    int jdbc_close_connect();


    /** 设置是否自动提交事务 */
    int jdbc_set_auto_commit(bool auto_commit);
    /** 提交事务 */
    int jdbc_commit();
    /** 创建一个保存点 */
    sql::Savepoint* jdbc_save_point(const std::string& name);
    /** 回滚到一个保存点 */
    int jdbc_rollback_save_point(sql::Savepoint* point);
    /** 释放保存点, 必须调用(内存泄漏) */
    int jdbc_release_save_point(sql::Savepoint* point);


    /** stmt: setXXX(index, data), index是从1开始
    返回值: 受影响的行数 affected_rows , 修改没有发生变化, 会返回0  */
    int jdbc_executeUpdate(const std::string& sql,
        std::function<void (sql::PreparedStatement* stmt)> prepCallBack,
        std::function<void (sql::SQLException &e)> exceptionCallBack);

    /** stmt: setXXX(index, data), index是从1开始
    返回值: 0: 成功  */
    int jdbc_executeQuery(const std::string& sql,
        std::function<void (sql::PreparedStatement* stmt)> prepCallBack,
        std::function<void (sql::ResultSet* result)> resultCallBack,
        std::function<void (sql::SQLException &e)> exceptionCallBack);

protected:
    void printSQLException(sql::SQLException &e);

private:
    std::string host;
    std::string username;
    std::string passwd;
    std::string database;

    sql::Driver* driver;
    sql::Connection* conn;
};
View Code

相关文章: