zoukankan      html  css  js  c++  java
  • mysql c++ jdbc 示例

    使用注意事项

     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

    JdbcHelper.cpp

    #include "JdbcHelper.h"
    #include <cppconn/parameter_metadata.h>
    
    
    JdbcHelper::JdbcHelper(const std::string& host, const std::string& username, const std::string& passwd, const std::string& database)
    {
        this->host = host;
        this->username = username;
        this->passwd = passwd;
        this->database = database;
    
        this->conn = NULL;
    
        try {
            this->driver = get_driver_instance();
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "get_driver_instance");
        }
        
        if (driver == NULL) {
            std::cout << "driver is null" << std::endl;
        }
    }
    
    
    JdbcHelper::~JdbcHelper()
    {
    }
    
    
    int JdbcHelper::jdbc_connect(bool enableSSL)
    {
        if (NULL != conn) {
            return -1;
        }
        this->m_enableSSL = enableSSL;
    
        try {
            sql::ConnectOptionsMap opts;
            opts["hostName"] = this->host;
            opts["userName"] = this->username;
            opts["password"] = this->passwd;
            if (enableSSL) {
                // 默认使用SSL
            }
            else {
                opts["OPT_SSL_MODE"] = sql::SSL_MODE_DISABLED;
            }
            opts["OPT_RECONNECT"] = sql::ConnectPropertyVal(true);
    
            this->conn = driver->connect(opts);
    
            if (NULL == conn) {
                printf("conn id null
    ");
                return -2;
            }
    
            // 选择数据库
            sql::SQLString catalog(this->database);
            conn->setSchema(catalog);
    
            return 0;
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "connect");
        }
    
        return -3;
    }
    
    
    int JdbcHelper::jdbc_close_connect()
    {
        if (conn) {
            conn->close();
            delete conn;
            conn = NULL;
    
            return 0;
        }
    
        return -1;
    }
    
    bool JdbcHelper::jdbc_is_connected()
    {
        if (conn) {
            return (conn->isClosed() == false);
        }
        else {
            this->jdbc_connect(m_enableSSL);
        }
    
        return false;
    }
    
    int JdbcHelper::jdbc_set_auto_commit(bool auto_commit)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
    
        try {
            conn->setAutoCommit(auto_commit);
            return 0;
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "setAutoCommit");
        }
    
        return -2;
    }
    
    int JdbcHelper::jdbc_commit()
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
    
        try {
            conn->commit();
            return 0;
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "commit");
        }
    
        return -2;
    }
    
    sql::Savepoint* JdbcHelper::jdbc_save_point(const std::string& name)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return NULL;
        }
    
        try {
            sql::SQLString savePointName(name);
            return conn->setSavepoint(savePointName);
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "setSavepoint");
        }
    
        return NULL;
    }
    
    int JdbcHelper::jdbc_rollback_save_point(sql::Savepoint* point)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
        if (NULL == point) {
            return -2;
        }
    
        try {
            conn->rollback(point);
            return 0;
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "rollback");
        }
    
        return -3;
    }
    
    int JdbcHelper::jdbc_release_save_point(sql::Savepoint* point)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
        if (NULL == point) {
            return -2;
        }
    
        int rc = -1;
        try {
            conn->releaseSavepoint(point);
            rc = 0;
        }
        catch (sql::SQLException &e) {
            this->printSQLException(e, "releaseSavepoint");
            rc = -3;
        }
    
        delete point;
    
        return rc;
    }
    
    int JdbcHelper::jdbc_executeUpdate(
        const std::string& sql,
        std::function<void(sql::PreparedStatement* stmt)> prepCallBack,
        std::function<void(sql::SQLException &e)> exceptionCallBack)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
    
        int row_affected = -1;
        sql::PreparedStatement* stmt = NULL;
    
        try {
            do
            {
                sql::SQLString sqlString(sql);
                stmt = conn->prepareStatement(sqlString);
                if (NULL == stmt) {
                    row_affected = -2;
                    break;
                }
    
                sql::ParameterMetaData* paramMetaData = stmt->getParameterMetaData();
                if (paramMetaData) {
                    if (paramMetaData->getParameterCount() > 0) {
                        if (prepCallBack) {
                            prepCallBack(stmt);
                        }
                    }
                }
    
                row_affected = stmt->executeUpdate(); // 插入数据
            } while (false);
        }
        catch (sql::SQLException &e)
        {
            this->printSQLException(e, sql);
    
            if (exceptionCallBack) {
                exceptionCallBack(e);
            }
        }
    
        if (stmt) {
            try {
                stmt->close();
            }
            catch (sql::SQLException &e) {
                this->printSQLException(e, "close stmt res");
            }
            delete stmt;
        }
    
        return row_affected;
    }
    
    
    int JdbcHelper::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)
    {
        if (NULL == conn || conn->isClosed()) {
            printf("jdbc conn is closed
    ");
            return -1;
        }
    
        int rc = -1;
        sql::PreparedStatement* prep_stmt = NULL;
        sql::ResultSet* res = NULL;
    
        try {
            do
            {
                // std::cout << "SQL: " << sql << std::endl;
                sql::SQLString sqlString(sql);
    
                prep_stmt = conn->prepareStatement(sqlString);
                if (NULL == prep_stmt) {
                    rc = -2;
                    break;
                }
    
                if (prepCallBack) {
                    sql::ParameterMetaData* paramMetaData = prep_stmt->getParameterMetaData();
                    if (paramMetaData && paramMetaData->getParameterCount() > 0) {
                        prepCallBack(prep_stmt);
                    }
                }
    
                res = prep_stmt->executeQuery(); // 查询数据
                if (NULL == res) {
                    rc = -3;
                    break;
                }
    
                if (resultCallBack) {
                    sql::ResultSetMetaData* metaData = prep_stmt->getMetaData();
                    if (metaData && metaData->getColumnCount() > 0) {
                        // printSQLMeta(metaData);
                        resultCallBack(res);
                    }
                }
                
                rc = 0;
            } while (false);
        }
        catch (sql::SQLException &e)
        {
            // if (e.getErrorCode() == CR_SERVER_LOST) { } // in errmsg.h of mysqlclient
            printSQLException(e, sql);
    
            if (exceptionCallBack) {
                exceptionCallBack(e);
            }
        }
    
        if (prep_stmt) {
            try {
                prep_stmt->close();
            }
            catch (sql::SQLException &e) {
                this->printSQLException(e, "close stmt");
            }
            delete prep_stmt;
        }
        if (res) {
            try {
                res->close();
            }
            catch (sql::SQLException &e) {
                this->printSQLException(e, "close stmt res");
            }
            delete res;
        }
    
        return rc;
    }
    
    void JdbcHelper::printSQLException(sql::SQLException &e, const std::string& sql)
    {
        std::cout << "message: " << e.what() << std::endl;
        std::cout << "code: " << e.getErrorCode() << std::endl;
        std::cout << "state: " << e.getSQLState() << std::endl;
        std::cout << "sql: " << sql << std::endl;
    }
    
    void JdbcHelper::printSQLMeta(sql::ResultSetMetaData* metaData)
    {
        int columnCount = metaData->getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            std::cout << "column: " << i << ", name: " << metaData->getColumnName(i) << ", type: " << metaData->getColumnTypeName(i) << std::endl;
        }
    }
    View Code

    main.cpp

    /**
      g++ main.cpp JdbcHelper.cpp -std=c++11 -I/usr/local/mysql_cpp/include/jdbc -L/usr/local/mysql_cpp/lib64 -lmysqlcppconn -lssl -lcrypto
    */
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    
    #include "JdbcHelper.h"
    
    #ifdef _MSC_VER
    #pragma comment(lib, "mysqlcppconn.lib")
    #endif // _MSC_VER
    
    
    int main(int argc, char* argv[])
    {
        int data = 3;
    
        JdbcHelper jdbc("tcp://192.168.6.80:23306", "root", "123456", "test");
        jdbc.jdbc_connect(true);
    
        std::string sql = "SELECT id, label FROM test WHERE id = ?";
        jdbc.jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
            stmt->setInt(1, data);
        }, [](sql::ResultSet* result) {
    
            while (result->next())
            {
                int64_t id = result->getInt64(1);
                sql::SQLString label = result->getString(2);
    
                std::cout << "id: " << id << "  label: " << label << std::endl;
            }
        }, NULL);
    
    
        sql = "INSERT INTO test(id, label) VALUES(?, ?)";
        int affected_rows = jdbc.jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
            stmt->setInt64(1, 1234);
            stmt->setString(2, "label_1234");
        }, [sql](sql::SQLException &e) {
            std::cerr << "exception, SQL: " << sql << std::endl;
        });
    
        std::cout << "affected_rows:  " << affected_rows << std::endl;
    
        jdbc.jdbc_close_connect();
    
        return 0;
    }
    View Code
  • 相关阅读:
    3D数学 矩阵的更多知识(5)
    D3D中的光照(1)
    双节棍(C语言版)
    D3D中的Alpha融合技术(1)
    D3D编程必备的数学知识(5)
    Direct3D中的绘制(1)
    初始化Direct3D(2)
    D3D中的纹理映射(2)
    Direct3D中的绘制(1)
    D3D编程必备的数学知识(2)
  • 原文地址:https://www.cnblogs.com/baigoogledu/p/10155911.html
Copyright © 2011-2022 走看看