package cn.lfd.Utils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JdbcUtils_c3p0 {private static ComboPo
目录结构
.
|-- Makefile
|-- include
| |-- mysql_conn.h
| `-- sns_concern.h
|-- lib
| |-- Db
| | |-- Makefile
| | `-- mysql_conn.cpp
| |-- Makefile
| |-- sns_concern.cpp#pragma comment(lib,"E:\\mysql-connector-c-noinstall-6.0.2-win32\\lib\\libmysql.lib")#include#include#include#include using namespace std;class MyDB{public:MyDB();~MyDB();bool initDB(strin
| `-- sns_concern.o
|-- ma2
|-- ma2.cpp
`-- ma2.o
头文件:
#ifndef MYSQL_CONN_H
#define MYSQL_CONN_H
#include
#include
#include
using namespace std;
using namespace mysqlpp;
class mypool: public mysqlpp::ConnectionPool
{
public:
char * _user;
char * _pwd;
char * _host;
char * _db;
mypool(char* user, char* pwd, char* host, char* db) ;
~mypool();
Connection* create();
void destroy(Connection *cp);
unsigned int max_idle_time();
int get_pool_size();
};
class mysql_conn
{
public:
mypool *myptr;
Connection *con;
mysql_conn(string dbid, char *charset);
~mysql_conn();
StoreQueryResult mysql_query(string query_str, SQLQueryParms params=NULL);
int mysql_execute(string query_str,SQLQueryParms params=NULL);
int get_insert_id();
static mysql_conn get_instance(string dbid);
void hello();
int commit();
int rollback();
};
#endif
cpp文件
#include
#include
#include
#include "mysql_conn.h"
using namespace std;
using namespace mysqlpp;
mypool::mypool(char* user, char* pwd, char* host, char* db)
{
_user = user;
_pwd = pwd;
_host = host;
_db = db;
}
mypool::~mypool()
{
clear();
}
Connection* mypool::create()
{
return new Connection(_db, _host, _user, _pwd);
}
void mypool::destroy(Connection *cp)
{
delete cp;
}
unsigned int mypool::max_idle_time()
{
return 60;
}
int mypool::get_pool_size()
{
return this->size();
}
map dbpoolmap;
mysql_conn mysql_conn::get_instance(string dbid)
{
char *user = "poker_wrt";
char *pwd = "poker_wrt";
char *host = "192.168.0.246";
char *db = "test";
char *charset = "gbk";
if(dbpoolmap.count(dbid) == 0)
{
dbpoolmap[dbid] = 0;
}
if (0 == dbpoolmap[dbid])
{
dbpoolmap[dbid] = new mypool(user, pwd, host, db);
}
mysql_conn instance(dbid,charset);
return instance;
}
mysql_conn::mysql_conn(string dbid, char *charset)
{
myptr = dbpoolmap[dbid];
con = myptr->grab();
string str = "set names " ;
str = str + charset;
this->mysql_execute(str);
this->mysql_execute("set autocommit=0");
}
mysql_conn::~mysql_conn()
{
cout << "in destruct func" << endl;
myptr->release(con);
}
void mysql_conn::hello()
{
cout << "from hello" << endl;
}
StoreQueryResult mysql_conn::mysql_query(string query_str, SQLQueryParms params)
{
Query query = con->query(query_str);
query.parse();
StoreQueryResult res = query.store(params) ;
return res;
}
int mysql_conn::mysql_execute(string query_str,SQLQueryParms params)
{
Query query = con->query(query_str);
query.parse();
query.execute(params);
return query.affected_rows();
}
int mysql_conn::get_insert_id()
{
Query query = con->query();
return query.insert_id();
}
int mysql_conn::commit()
{
Transaction trans(*con);
trans.commit();
return 1;
}
int mysql_conn::rollback()
{
Transaction trans(*con);
trans.rollback();
return 1;
}
代码在附件中