Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. //VLR cpp with main
  2. /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7.  
  8. There are special exceptions to the terms and conditions of the GPL
  9. as it is applied to this software. View the full text of the
  10. exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
  11. software distribution.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22.  
  23. /* @README A
  24. - Connect to dbdev.cs.kent.edu server using your flashline username and password
  25. - place this file (CompleteExample2.cpp) on your home directory or
  26. anywhere, this is only for testing purposes
  27. */
  28.  
  29. #include <stdlib.h>
  30. #include <iostream>
  31. #include <string>
  32. #include <vector>
  33.  
  34. /*
  35. Include directly the different
  36. headers from cppconn/ and mysql_driver.h + mysql_util.h
  37. (and mysql_connection.h). This will reduce your build time!
  38. */
  39. #include "mysql_connection.h"
  40.  
  41. #include <cppconn/driver.h>
  42. #include <cppconn/exception.h>
  43. #include <cppconn/resultset.h>
  44. #include <cppconn/statement.h>
  45. #include <cppconn/prepared_statement.h>
  46.  
  47. using namespace std;
  48.  
  49. void addUser_();
  50. void addCandidate_();
  51. void dropUser_();
  52. void dropCandidate_();
  53. bool verifyExistence_(string);
  54. void vote_(string);
  55. bool verifyPassword_(string, string);
  56. void displayVote_();
  57.  
  58. int main() {
  59. sql::Driver *driver;
  60. sql::Connection *con;
  61. sql::Statement *stmt;
  62. sql::ResultSet *res;
  63. sql::PreparedStatement *pstmt;
  64. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  65. //sql::ResultSet *res2;
  66. //sql::Statement *stmt2;
  67.  
  68. bool run = true;
  69. bool voteAgain = true;
  70. std::vector<string> voteConstraint_;
  71. voteConstraint_.push_back("setUp");
  72.  
  73. driver = get_driver_instance();
  74. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  75. con->setSchema("ajacob18");
  76.  
  77. stmt = con->createStatement();
  78. res = stmt->executeQuery("SELECT * from candidate");
  79.  
  80. while (run == true) {
  81.  
  82. cout << "Do you have an account? (Y/N) ";
  83. string answer, username, password, voteAgainString;
  84. cin >> answer;
  85.  
  86. //Asking if currently have user
  87. if (answer == "Y") {
  88. cout << endl << "Enter username: ";
  89. cin >> username;
  90.  
  91. //Admin user
  92. bool continue_ = true;
  93. if (username == "admin") {
  94. string adminPassword, more;
  95. cout << "Enter admin password(password is password): ";
  96. cin >> adminPassword;
  97.  
  98. if (adminPassword == "password") {
  99. int choice;
  100. while (continue_ == true) {
  101. cout << "What would you like to do?" << endl;
  102. cout << "Add User(1), Add Candidate(2), Drop User(3), Drop Candidate(4), Display Votes(5)" << endl;
  103. cin >> choice;
  104.  
  105. switch(choice) {
  106. case 1: {addUser_(); break;}
  107. case 2: {addCandidate_(); break;}
  108. case 3: {dropUser_(); break;}
  109. case 4: {dropCandidate_(); break;}
  110. case 5: {displayVote_(); break;}
  111. default: {cout << "Did not enter a choice"; break;}
  112.  
  113.  
  114.  
  115. }
  116.  
  117. cout << "Would you like to do more?(Y/N) ";
  118. cin >> more;
  119. if (more == "N")
  120. continue_ = false;
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131. //verifying username
  132. if (verifyExistence_(username) == true && username != "admin") {
  133. cout << endl << "Enter Password: ";
  134. cin >> password;
  135.  
  136. //verifying password
  137. if (verifyPassword_(username, password) == true) {
  138. while (voteAgain == true) {
  139.  
  140. //stmt = con->createStatement();
  141. //res = stmt->executeQuery("SELECT * from candidate");
  142. while (res->next()) {
  143. cout << res->getString("username") << endl;
  144. }
  145.  
  146. cout << endl << "Enter username to vote: ";
  147. cin >> username;
  148.  
  149. //give vector a length
  150. //voteConstraint_.push_back("setUp");
  151.  
  152. for (int i = 0; i < voteConstraint_.size(); i++) {
  153. cout << voteConstraint_.size() << endl;
  154. if (username == voteConstraint_[i]) {
  155. cout << endl << "Already voted for!";
  156. voteAgain = false;
  157. }
  158. }
  159. cout << voteConstraint_.size() << endl;
  160. if (voteAgain == true) {
  161. voteConstraint_.push_back(username);
  162. vote_(username);
  163. }
  164.  
  165. //voteConstraint_.push_back(username);
  166. cout << "Would you like to vote again?";
  167. cin >> voteAgainString;
  168. if (voteAgainString == "N")
  169. voteAgain = false;
  170. }
  171. }
  172. else
  173. cout << "Incorrect password!" << endl;
  174. }
  175. else
  176. cout << "Username Incorrect";
  177.  
  178. }
  179. else
  180. addUser_();
  181.  
  182. //keep program running
  183. cout << endl << "Run again? (Y/N) ";
  184. char V;
  185. cin >> V;
  186. if (V == 'N')
  187. run = false;
  188.  
  189. }
  190. }
  191.  
  192. void displayVote_() {
  193. sql::Driver *driver;
  194. sql::Connection *con;
  195. driver = get_driver_instance();
  196. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  197. con->setSchema("ajacob18");
  198.  
  199. sql::Statement *stmt;
  200. sql::ResultSet *res;
  201.  
  202. std::auto_ptr< sql::PreparedStatement > pstmt;
  203. stmt = con->createStatement();
  204. res = stmt->executeQuery("SELECT * from vote");
  205. while (res->next()) {
  206. cout << res->getString("username") << " " << res->getInt("vote") << endl;
  207.  
  208. }
  209. };
  210.  
  211. void addUser_() {
  212. sql::Driver *driver;
  213. sql::Connection *con;
  214. driver = get_driver_instance();
  215. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  216. con->setSchema("ajacob18");
  217.  
  218. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  219. string username, password, first_name, last_name, email;
  220. cout << "Insert username, password, first name, last name and email: ";
  221. cin >> username >> password >> first_name >> last_name >> email;
  222. cout << endl;
  223.  
  224. vector<string> username_vector;
  225. username_vector.push_back(username);
  226. username_vector.push_back(password);
  227. username_vector.push_back(first_name);
  228. username_vector.push_back(last_name);
  229. username_vector.push_back(email);
  230.  
  231. pstmtptr.reset(con->prepareStatement("CALL add_username(?,?,?,?,?)"));
  232.  
  233. pstmtptr->setString(1, username_vector[0]);
  234. pstmtptr->setString(2, username_vector[1]);
  235. pstmtptr->setString(3, username_vector[2]);
  236. pstmtptr->setString(4, username_vector[3]);
  237. pstmtptr->setString(5, username_vector[4]);
  238. pstmtptr->execute();
  239.  
  240. delete con;
  241. pstmtptr.reset();
  242. };
  243.  
  244. void addCandidate_() {
  245. sql::Driver *driver;
  246. sql::Connection *con;
  247. driver = get_driver_instance();
  248. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  249. con->setSchema("ajacob18");
  250.  
  251. sql::Statement *stmt;
  252. sql::ResultSet *res;
  253.  
  254. std::auto_ptr< sql::PreparedStatement > pstmt;
  255. string votee;
  256. stmt = con->createStatement();
  257. res = stmt->executeQuery("SELECT * from user");
  258. while (res->next()) {
  259. cout << res->getString("username") << endl;
  260. }
  261. cout << endl << "Who do you want to add? " << endl;
  262. cin >> votee;
  263.  
  264. pstmt.reset(con->prepareStatement("CALL add_candidate(?)"));
  265. pstmt->setString(1, votee);
  266. pstmt->execute();
  267.  
  268. };
  269.  
  270. void dropUser_() {
  271. sql::Driver *driver;
  272. sql::Connection *con;
  273. driver = get_driver_instance();
  274. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  275. con->setSchema("ajacob18");
  276.  
  277. sql::Statement *stmt;
  278. sql::ResultSet *res;
  279.  
  280. std::auto_ptr< sql::PreparedStatement > pstmt;
  281. string votee;
  282. cout << "Who do you want to add? " << endl;
  283. stmt = con->createStatement();
  284. res = stmt->executeQuery("SELECT * from user");
  285. while (res->next()) {
  286. cout << res->getString("username") << endl;
  287. }
  288. cin >> votee;
  289.  
  290. pstmt.reset(con->prepareStatement("CALL drop_user(?)"));
  291. pstmt->setString(1, votee);
  292. pstmt->execute();
  293.  
  294. };
  295.  
  296. void dropCandidate_() {
  297. sql::Driver *driver;
  298. sql::Connection *con;
  299. driver = get_driver_instance();
  300. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  301. con->setSchema("ajacob18");
  302. sql::Statement *stmt;
  303. sql::ResultSet *res;
  304.  
  305. std::auto_ptr< sql::PreparedStatement > pstmt;
  306. string votee;
  307. cout << "Who do you want to delete? " << endl;
  308. stmt = con->createStatement();
  309. res = stmt->executeQuery("SELECT * from candidate");
  310. while (res->next()) {
  311. cout << res->getString("username") << endl;
  312. }
  313. cin >> votee;
  314. cout << endl;
  315.  
  316. pstmt.reset(con->prepareStatement("CALL drop_candidate(?)"));
  317. pstmt->setString(1, votee);
  318. pstmt->execute();
  319. };
  320.  
  321. bool verifyExistence_(string username) {
  322. sql::Driver *driver;
  323. sql::Connection *con;
  324. driver = get_driver_instance();
  325. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  326. con->setSchema("ajacob18");
  327. std::auto_ptr<sql::Statement> stmt(con->createStatement());
  328. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  329.  
  330. pstmtptr.reset(con->prepareStatement("CALL username_exists(?, @count)"));
  331. pstmtptr->setString(1, username);
  332. pstmtptr->execute();
  333.  
  334. std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @count AS username_count"));
  335. if (res->next()){
  336. if (res->getInt("username_count") == 0)
  337. return false;
  338. else
  339. return true;
  340. }
  341. delete con;
  342. pstmtptr.reset();
  343. };
  344.  
  345. bool verifyPassword_(string username, string password) {
  346. sql::Driver *driver;
  347. sql::Connection *con;
  348. driver = get_driver_instance();
  349. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  350. con->setSchema("ajacob18");
  351. std::auto_ptr<sql::Statement> stmt(con->createStatement());
  352. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  353.  
  354. pstmtptr.reset(con->prepareStatement("CALL password_correct(?, ?, @count)"));
  355. pstmtptr->setString(1, username);
  356. pstmtptr->setString(2, password);
  357. pstmtptr->execute();
  358.  
  359. std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @count AS password_count"));
  360. if (res->next()){
  361. if (res->getInt("password_count") == 1)
  362. return true;
  363. else
  364. return false;
  365. }
  366. delete con;
  367. pstmtptr.reset();
  368. };
  369.  
  370.  
  371. void vote_(string votee) {
  372. sql::Driver *driver;
  373. sql::Connection *con;
  374. sql::ResultSet *res;
  375. driver = get_driver_instance();
  376. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  377. con->setSchema("ajacob18");
  378.  
  379. std::auto_ptr< sql::PreparedStatement > pstmt;
  380. sql::Statement *stmt;
  381.  
  382. pstmt.reset(con->prepareStatement("CALL vote(?)"));
  383. pstmt->setString(1, votee);
  384. pstmt->execute();
  385.  
  386. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement