天天看点

mysql连接池c++语言实现(2)

main.cpp

#include "dbpool.h"
#include <stdio.h>
 
/*--------------------------------------------------------------
    单例模式,全局唯一 db pool,程序中使用onnpool中获取一个
    db连接使用,使用完之后调用ReleaseConnection把conn放回pool中去.
----------------------------------------------------------------*/
DBPool  connpool = DBPool::GetInstance();
 
int main(int argc, char* argv[])
{
    //初始化连接,创建参数中maxSize一半的连接
    connpool.initPool("tcp://127.0.0.1:3306", "root", "123456", 100);
 
    Connection *con;
    Statement *state;
    ResultSet *result;
    con = connpool.GetConnection();//get a db conn
    for(int i = 0; i<1000; i++)
    {
        state = con->createStatement();
        state->execute("use mysql");
 
        // 查询
        result = state->executeQuery("select host,user from user");
 
        // 输出查询
        while (result->next())
        {
            try{
                string user = result->getString("user");
                string name = result->getString("host");
                cout << user << " : " << name << endl;
            }catch(sql::SQLException& e){
                std::cout << e.what() << std::endl;
            }
        }
 
        /*result = state->executeQuery("select cust_id,cust_name from customers");
        while (result->next())
        {
            try{
                string user = result->getString("cust_id");
                string name = result->getString("cust_name");
                cout << user << " : " << name << endl;
            }catch(sql::SQLException& e){
              std::cout << e.what() << std::endl;
            }
        }
    */
        std::cout << i << std::endl;
 
    }
 
    delete result;
    delete state;
    connpool.ReleaseConnection(con);//注意,con用完之后一定要记得归还
 
    return 0;
}