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;
}