C + + after learning, let me have a lot of questions about C + +, first, the virtual function in C + +, know that the virtual function table in memory exists in the address of the class instance in the first address. After learning the Java connection database, leisurely I produced an idea is not able to connect to the database through C + +, so I found the relevant code on the Internet and connected to the database. (1) Install MySQL server on this machine, the installation process is not elaborate. (2) After decompression mysql++, generally used is the VS2008 project to Uppdate. (3) The next step is to write our code to operate the database, of course, before this, your MySQL service to install, and to create a database and tables to use to operate.
(Code Reference) #include <winsock2.h>//Because you want to use the socket, you need to include the socket2.1 header file #include "mysql++.h"//mysql++ header file #include <st Ring> #include <iostream> using namespace MYSQLPP; using namespace Std;
Const DWORD SPACETIME = 20 * 60 * 1000; 20-minute const int len_name = 8; const char cch[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random string dictionary
char* Rand_password (char* str, int len) {int i = 0, n = 0; int nlength = strlen (CCH); for (i = 0, n = 0; i < len_name && n < nlength; ++i, ++n) {int x = rand ()% (nlength–1); Str[i] = cch[x]; }
str[i + 1] = ' + '; return str; }
int _tmain (int argc, _tchar* argv[]) {std::cout << "Vpn_modify_mysql_user_password Tool" << Std::endl; Std::cout << "Copyright (C) Eliteyang" << Std::endl; Std::cout << "http://www.cppfans.org" << Std::endl;
Mysqlpp::connection _vpnconn; Set the database encoding _vpnconn.set_option (new mysqlpp::setcharsetnameoption ("UTF8"));
String Dbip, Dbusername, Dbpwd, DbName; cout << "\n\n\nmysql DataBase Info Input" << Endl; cout << "Please enter the MySQL server IP address:"; Cin >> Dbip;
cout << "Please enter MySQL Administrator account:"; Cin >> Dbusername;
cout << "Please enter the MySQL Administrator account password:"; Cin >> Dbpwd;
cout << "Please enter MySQL database name:"; Cin >> DbName;
cout << "Default port number is 3306" << Endl;
BOOL Bconnect = _vpnconn.connect (Dbname.c_str (), Dbip.c_str (), Dbusername.c_str (), Dbpwd.c_str (), 3306); if (!bconnect) {cout << VPN Connect MySQL failed! Error: "<< _vpnconn.error () << Endl; System ("Pause"); return-1; } else {cout << "VPN Connect mysql success!" << Endl; }
DWORD timeslot = timeGetTime (); Char szchar[256] = {0}; String strName = "123"; cout << "Please enter the user name to change the password regularly:"; Cin >> StrName;
sprintf_s (Szchar, "select * from Vpn_user_info where username=%s", strname.c_str ()); while (true) {if (timeGetTime () –timeslot < spacetime) {continue;} try {mysqlpp::query _query = _vpnconn.query (Szchar); Mysqlpp::storequeryresult _result = _query.store (); if (_result.num_rows ()! = 1) {cout << "username[123" Repeat, please check "<< Endl; timeslot = timeGetTime (); Continue }
String strusername = _result[0][0].c_str (); String Struserpassword = _result[0][1].c_str (); cout << "Curentinfo username[" << strusername.c_str () << "] password[" << struserpassword.c_str () << "]" << Endl;
Char strtemp[len_name + 1] = {0}; Struserpassword = Rand_password (strtemp, len_name);
char sztemp[256] = {0}; sprintf_s (sztemp, "UPDATE vpn_user_info SET Password = '%s ' WHERE UserName = '%s ';", Struserpassword.c_str (), Strus Ername.c_str ()); _query << sztemp << Endl; _query.execute (); cout << "Modifyuserinfo username[" << strusername.c_str () << "] password[" << struserpass WORD.C_STR () << "]" << Endl; } catch (const mysqlpp::badquery& er) { //Handle any query errors cerr << "query Error: "<< er.what () << Endl; return-1; } catch (const mysqlpp::badconversion& er) { //Handle bad conversions cerr << "Co Nversion error: "<< er.what () << Endl << " \tretrieved data size: "<< er.retrieved << ", Actual size:" << er.actual_size << Endl; return-1; } catch (const mysqlpp::exception& er) { //catch-all for aNY Other mysql++ exceptions cerr << "Error:" << er.what () << Endl; return-1; } }
_vpnconn.disconnect (); System ("Pause");
return 0; }
The previous use of a simple random string generation function, and set the database code to implement the MySQL server IP address; Administrator account; Administrator account password; database name; port number 3306.
Finally, with more catch, in order to catch more anomalies,
C + + Insights