MySQL Connector/C++ 接口实例

 mysql的官方网站有对MySQL Connector/C++的文档解释和具体实例,但是大家也知道,如果把那些具体实例的代码只是生硬的套入项目工程中是万万不行的,因为项目安全性要求,需要对容错,资源创建释放问题严格关注,下面贴一个自己的方法函数,里面涵盖了相关安全处理而且对调用存储过程也有所涉及:
 
 
 
  1. bool CommonService::......(JSONNode& in, JSONNode& out) 
  2. {    
  3.     /*=====校验json传入参数=====*/ 
  4. ..........
  5.  
  6.     /*=====解析json传入参数,得到安装码或者id, 安装时间, 硬盘序列号, ip,mac=====*/ 
  7.  
  8.    ..........
  9.  
  10.     /*=====通过安装码找到数据库中的对应记录写入传入的值=====*/ 
  11.     /********数据库操作**********/ 
  12.     //1.得到数据库连接 
  13.     Connection* con = G<ConnectionPool>().GetConnection(); 
  14.     if (con == NULL) 
  15.     { 
  16.         LOG4CXX_ERROR(g_logger, "不能得到数据库连接"); 
  17.         out.push_back(JSONNode(RESULT, ACTION_FALSE)); 
  18.         out.push_back(JSONNode(ERROR_MESSAGE, "不能得到数据库连接")); 
  19.         return false
  20.     } 
  21.  
  22.     int ret = 1; 
  23.     PreparedStatement* prep_stmt = NULL; 
  24.     ResultSet*         res       = NULL; 
  25.  
  26.     try 
  27.     { 
  28.         con->setSchema(G<ConnectionPool>().GetDBName().c_str()); 
  29.  
  30.         //执行sql改变安装状态 
  31.         std::string sql_statement = "update tb_host set reg_date=?, sn=?, ip=?, mac=?, state=?, sync_state=? where reg_code =? and state=?";    //要执行的sql语句 
  32.         //事务处理 
  33.         con->setAutoCommit(0); 
  34.         prep_stmt = con->prepareStatement(sql_statement.c_str()); 
  35.  
  36.         prep_stmt->setString(1, install_time.c_str()); 
  37.         prep_stmt->setString(2, harddrive_sn.c_str()); 
  38.         prep_stmt->setString(3, ip_address.c_str()); 
  39.         prep_stmt->setString(4, mac_address.c_str()); 
  40.         prep_stmt->setInt(5, HAS_INSTALL); 
  41.         prep_stmt->setInt(6, HAS_SYNC); 
  42.         prep_stmt->setString(7, install_code.c_str()); 
  43.         prep_stmt->setInt(8, NO_INSTALL); 
  44.  
  45.         if(prep_stmt->executeUpdate() == 0) 
  46.         { 
  47.             ret = 2; 
  48.             LOG4CXX_INFO(g_logger, "....."); 
  49.             out.push_back(JSONNode(ERROR_MESSAGE, ".....")); 
  50.             goto Finally_handle; 
  51.         } 
  52.         //调用赋默认策略存储过程 
  53.         std::string procedure = "CALL updateHostPolicyByModHost(?,?, @ret, @msg)"
  54.         prep_stmt = con->prepareStatement(procedure.c_str()); 
  55.         prep_stmt->setString(1, install_code.c_str()); 
  56.         prep_stmt->setInt(2, 0); 
  57.         prep_stmt->execute(); 
  58.  
  59.         std::string query = "select @ret AS ret,@msg AS msg"
  60.         prep_stmt = con->prepareStatement(query.c_str()); 
  61.         res = prep_stmt->executeQuery(); 
  62.         while(res->next()) 
  63.         { 
  64.             if(res->getInt("ret") != 0) 
  65.             { 
  66.                 LOG4CXX_ERROR(g_logger, "....." << res->getString("msg").c_str() << res->getInt("ret")); 
  67.                 out.push_back(JSONNode(ERROR_MESSAGE, ".....")); 
  68.                 goto Finally_handle; 
  69.             } 
  70.         } 
  71.         con ->commit(); 
  72.  
  73.     } 
  74.     catch (SQLException& e) 
  75.     { 
  76.         try 
  77.         { 
  78.             con->rollback(); 
  79.         } 
  80.         catch (SQLException& e) 
  81.         { 
  82.             ret = 0; 
  83.             LOG4CXX_ERROR(g_logger, "数据库异常" << e.what()); 
  84.         } 
  85.  
  86.         ret = 0; 
  87.         LOG4CXX_ERROR(g_logger, "数据库异常" << e.what()); 
  88.         out.push_back(JSONNode(ERROR_MESSAGE, e.what()));  
  89.     } 
  90.     catch (...) 
  91.     { 
  92.         ret = 0; 
  93.         LOG4CXX_ERROR(g_logger, "其他错误"); 
  94.         out.push_back(JSONNode(ERROR_MESSAGE, "其他错误")); 
  95.     } 
  96.      
  97. Finally_handle: 
  98.     DestorySql(res, prep_stmt); 
  99.     //将连接释放到连接池 
  100.     G<ConnectionPool>().ReleaseConnection(con); 
  101.  
  102.     if (ret == 1) 
  103.     { 
  104.         out.push_back(JSONNode(RESULT, ACTION_SUCCESS)); 
  105.         return true
  106.     } 
  107.     else if (ret == 2) 
  108.     { 
  109.         out.push_back(JSONNode(RESULT, ACTION_FALSE)); 
  110.         return true
  111.     } 
  112.     else 
  113.     { 
  114.         out.push_back(JSONNode(RESULT, ACTION_FALSE)); 
  115.         return false
  116.     } 
  117.  
  118.  
  119.    
       
    1. /************************************************************************/ 
    2. /* 销毁数据库记录集资源                                                 */ 
    3. /************************************************************************/ 
    4. void CommonService::DestorySql(ResultSet* res, PreparedStatement* prep_stmt) 
    5.     if (res != NULL) 
    6.     { 
    7.         try 
    8.         { 
    9.             res ->close(); 
    10.         } 
    11.         catch(SQLException& e) 
    12.         { 
    13.             LOG4CXX_ERROR(g_logger, "数据库异常" << e.what()); 
    14.         } 
    15.         delete res; 
    16.         res = NULL; 
    17.     } 
    18.     if (prep_stmt != NULL){ 
    19.         try 
    20.         { 
    21.             prep_stmt->close(); 
    22.         } 
    23.         catch(SQLException& e) 
    24.         { 
    25.             LOG4CXX_ERROR(g_logger, "数据库异常" << e.what()); 
    26.         } 
    27.         delete prep_stmt; 
    28.         prep_stmt = NULL; 
    29.     } 
 
 

本文出自 “永远的朋友” 博客,请务必保留此出处http://yaocoder.blog.51cto.com/2668309/643004

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值