MySQL Connector/C ++ interface instance

Source: Internet
Author: User

The official mysql website provides instructions and examples for MySQL ctor/C ++ documentation, if the code for specific instances is just a raw plug-in to the project, it will never work. Because of the security requirements of the project, you need to pay close attention to fault tolerance and Resource Creation and release issues, next I will post a method function of my own, which covers related security processing and also involves calling the stored procedure:
 
 

Bool CommonService:... (JSONNode & in, JSONNode & out)
{
/* = Verify json input parameters === */
..........
 
/* ==== Parse json input parameters to get the installation code or id, installation time, hard disk serial number, ip address, mac ==== */
 
..........
 
/* ===== Find the input value of the corresponding record in the database through the installation code ===== */
**********/
// 1. Obtain the database connection
Connection * con = G <ConnectionPool> (). GetConnection ();
If (con = NULL)
{
LOG4CXX_ERROR (g_logger, "cannot get database connection ");
Out. push_back (JSONNode (RESULT, ACTION_FALSE ));
Out. push_back (JSONNode (ERROR_MESSAGE, "cannot get database connection "));
Return false;
}
 
Int ret = 1;
PreparedStatement * prep_stmt = NULL;
ResultSet * res = NULL;
 
Try
{
Con-> setSchema (G <ConnectionPool> (). GetDBName (). c_str ());
 
// Execute SQL to change the installation status
Std: string SQL _statement = "update tb_host set reg_date = ?, Sn = ?, Ip = ?, Mac = ?, State = ?, Sync_state =? Where reg_code =? And state =? "; // SQL statement to be executed
// Transaction Processing
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;
}
// Call the stored procedure with the Default policy
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, "database exception" <e. what ());
Goto Finally_handle;
}
 
Ret = 0;
LOG4CXX_ERROR (g_logger, "database exception" <e. what ());
Out. push_back (JSONNode (ERROR_MESSAGE, e. what ()));
Goto Finally_handle;
}
Catch (...)
{
Ret = 0;
LOG4CXX_ERROR (g_logger, "other errors ");
Out. push_back (JSONNode (ERROR_MESSAGE, "other errors "));
Goto Finally_handle;
}

Finally_handle:
DestorySql (res, prep_stmt );
// Release the connection to the connection pool
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;
}
 
}
 
/*************************************** *********************************/
/* Destroy database record set Resources */
/*************************************** *********************************/
Void CommonService: DestorySql (ResultSet * res, PreparedStatement * prep_stmt)
{
If (res! = NULL)
{
Try
{
Res-> close ();
}
Catch (SQLException & e)
{
LOG4CXX_ERROR (g_logger, "database exception" <e. what ());
}
Delete res;
Res = NULL;
}
If (prep_stmt! = NULL ){
Try
{
Prep_stmt-> close ();
}
Catch (SQLException & e)
{
LOG4CXX_ERROR (g_logger, "database exception" <e. what ());
}
Delete prep_stmt;
Prep_stmt = NULL;
}
}


 
 
This article is from the "Forever Friend" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.