您的位置:首页 > 编程语言 > C语言/C++

MySQL Connector/C++ 接口实例

2013-11-05 16:30 375 查看
mysql的官方网站有对MySQL Connector/C++的文档解释和具体实例,但是大家也知道,如果把那些具体实例的代码只是生硬的套入项目工程中是万万不行的,因为项目安全性要求,需要对容错,资源创建释放问题严格关注,下面贴一个自己的方法函数,里面涵盖了相关安全处理而且对调用存储过程也有所涉及:

bool CommonService::......(JSONNode& in, JSONNode& out)
{
/*=====校验json传入参数=====*/
..........

/*=====解析json传入参数,得到安装码或者id, 安装时间, 硬盘序列号, ip,mac=====*/

..........

/*=====通过安装码找到数据库中的对应记录写入传入的值=====*/
/********数据库操作**********/
//1.得到数据库连接
Connection* con = G<ConnectionPool>().GetConnection();
if (con == NULL)
{
LOG4CXX_ERROR(g_logger, "不能得到数据库连接");
out.push_back(JSONNode(RESULT, ACTION_FALSE));
out.push_back(JSONNode(ERROR_MESSAGE, "不能得到数据库连接"));
return false;
}

int ret = 1;
PreparedStatement* prep_stmt = NULL;
ResultSet*         res       = NULL;

try
{
con->setSchema(G<ConnectionPool>().GetDBName().c_str());

//执行sql改变安装状态
std::string sql_statement = "update tb_host set reg_date=?, sn=?, ip=?, mac=?, state=?, sync_state=? where reg_code =? and state=?";    //要执行的sql语句
//事务处理
con->setAutoCommit(0);
prep_stmt = con->prepareStatement(sql_statement.c_str());

prep_stmt->setString(1, install_time.c_str());
prep_stmt->setString(2, harddrive_sn.c_str());
prep_stmt->setString(3, ip_address.c_str());
prep_stmt->setString(4, mac_address.c_str());
prep_stmt->setInt(5, HAS_INSTALL);
prep_stmt->setInt(6, HAS_SYNC);
prep_stmt->setString(7, install_code.c_str());
prep_stmt->setInt(8, NO_INSTALL);

if(prep_stmt->executeUpdate() == 0)
{
ret = 2;
LOG4CXX_INFO(g_logger, ".....");
out.push_back(JSONNode(ERROR_MESSAGE, "....."));
goto Finally_handle;
}
//调用赋默认策略存储过程
std::string procedure = "CALL updateHostPolicyByModHost(?,?, @ret, @msg)";
prep_stmt = con->prepareStatement(procedure.c_str());
prep_stmt->setString(1, install_code.c_str());
prep_stmt->setInt(2, 0);
prep_stmt->execute();

std::string query = "select @ret AS ret,@msg AS msg";
prep_stmt = con->prepareStatement(query.c_str());
res = prep_stmt->executeQuery();
while(res->next())
{
if(res->getInt("ret") != 0)
{
LOG4CXX_ERROR(g_logger, "....." << res->getString("msg").c_str() << res->getInt("ret"));
out.push_back(JSONNode(ERROR_MESSAGE, "....."));
goto Finally_handle;
}
}
con ->commit();

}
catch (SQLException& e)
{
try
{
con->rollback();
}
catch (SQLException& e)
{
ret = 0;
LOG4CXX_ERROR(g_logger, "数据库异常" << e.what());
}

ret = 0;
LOG4CXX_ERROR(g_logger, "数据库异常" << e.what());
out.push_back(JSONNode(ERROR_MESSAGE, e.what()));
}
catch (...)
{
ret = 0;
LOG4CXX_ERROR(g_logger, "其他错误");
out.push_back(JSONNode(ERROR_MESSAGE, "其他错误"));
}

Finally_handle:
DestorySql(res, prep_stmt);
//将连接释放到连接池
G<ConnectionPool>().ReleaseConnection(con);

if (ret == 1)
{
out.push_back(JSONNode(RESULT, ACTION_SUCCESS));
return true;
}
else if (ret == 2)
{
out.push_back(JSONNode(RESULT, ACTION_FALSE));
return true;
}
else
{
out.push_back(JSONNode(RESULT, ACTION_FALSE));
return false;
}

}

[code]/************************************************************************/
/* 销毁数据库记录集资源                                                 */
/************************************************************************/
void CommonService::DestorySql(ResultSet* res, PreparedStatement* prep_stmt)
{
if (res != NULL)
{
try
{
res ->close();
}
catch(SQLException& e)
{
LOG4CXX_ERROR(g_logger, "数据库异常" << e.what());
}
delete res;
res = NULL;
}
if (prep_stmt != NULL){
try
{
prep_stmt->close();
}
catch(SQLException& e)
{
LOG4CXX_ERROR(g_logger, "数据库异常" << e.what());
}
delete prep_stmt;
prep_stmt = NULL;
}
}


[/code]

本文出自 “永远的朋友” 博客,请务必保留此出处http://yaocoder.blog.51cto.com/2668309/643004
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: