A c program that places certain user entered data into a dat
A c++ program that places certain user entered data into a database. (ie.
cout enter your name, birthday, age, and race
cin name, birthday, age, race
and only name age and race will be in the database)
Solution
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
*using MySQL Connector
*/
#include \"mysql_connection.h\"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>
using namespace std;
int main(void)
{
/*database variable */
string userName = \"root\";
string password = \"root\";
/* variables to store*/
string name,DOB,race;
int age;
cout<<\"Enter your name\";
cin>>name;
cout<<\"Enter your data of birth\";
cin>>DOB;
cout<<\"Enter your race\";
cin>>race;
cout>>\"Enter your age\";
cin>>age;
try {
sql::Driver *driver;
sql::Connection *con;
sql::PreparedStatement *pstmt;
/* Create a connection */
driver = get_driver_instance();
con =
driver->connect(\"tcp://127.0.0.1:3306\", userName, password);
/* Connect to the MySQL test database */
con->setSchema(\"test\"); /*in place of test you can place your database name or create with the same*/
/* \'?\' is the supported placeholder syntax */
/*here test is the table name which resides in the test database, you can put your table name*/
pstmt = con->prepareStatement(\"INSERT INTO test(name,age,race)VALUES (?,?,?)\");
pstmt->setString(1,name);
pstmt->setInt(2,age);
pstmt->setString(3,race);
pstmt->executeUpdate();
delete pstmt;
} catch (sql::SQLException &e) {
cout << \"# ERR: SQLException in \" << __FILE__;
cout << \"(\" << __FUNCTION__ << \") on line \" »
<< __LINE__ << endl;
cout << \"# ERR: \" << e.what();
cout << \" (MySQL error code: \" << e.getErrorCode();
cout << \", SQLState: \" << e.getSQLState() << »
\" )\" << endl;
}
return EXIT_SUCCESS;
}

