Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 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. bool verifyExistence_(string);
  51. void vote_(string);
  52.  
  53. int main() {
  54. sql::Driver *driver;
  55. sql::Connection *con;
  56. sql::Statement *stmt;
  57. sql::ResultSet *res;
  58. sql::PreparedStatement *pstmt;
  59. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  60. sql::ResultSet *res2;
  61. sql::Statement *stmt2;
  62.  
  63. bool run = true;
  64. bool voteAgain = true;
  65. std::vector<string> voteConstraint_;
  66. voteConstraint_.push_back("setUp");
  67.  
  68. driver = get_driver_instance();
  69. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  70. con->setSchema("ajacob18");
  71.  
  72. stmt = con->createStatement();
  73. res = stmt->executeQuery("SELECT * from candidate");
  74.  
  75. while (run == true) {
  76.  
  77. cout << "Do you have an account? (Y/N) ";
  78. string answer, username, password, voteAgainString;
  79. cin >> answer;
  80.  
  81. if (answer == "Y") {
  82. cout << endl << "Enter username: ";
  83. cin >> username;
  84. if (verifyExistence_(username) == true) {
  85.  
  86. cout << endl << "Enter Password: ";
  87. cin >> password;
  88. while (voteAgain == true) {
  89.  
  90. //stmt = con->createStatement();
  91. //res = stmt->executeQuery("SELECT * from candidate");
  92. while (res->next()) {
  93. cout << res->getString("username") << endl;
  94. }
  95.  
  96. cout << endl << "Enter username to vote: ";
  97. cin >> username;
  98.  
  99. //give vector a length
  100. //voteConstraint_.push_back("setUp");
  101.  
  102. for (int i = 0; i < voteConstraint_.size(); i++) {
  103. cout << voteConstraint_.size() << endl;
  104. if (username == voteConstraint_[i]) {
  105. cout << endl << "Already voted for!";
  106. voteAgain = false;
  107. }
  108. }
  109. cout << voteConstraint_.size() << endl;
  110. if (voteAgain == true) {
  111. voteConstraint_.push_back(username);
  112. vote_(username);
  113. }
  114.  
  115. //voteConstraint_.push_back(username);
  116. cout << "Would you like to vote again? ";
  117. cin >> voteAgainString;
  118. if (voteAgainString == "N")
  119. voteAgain = false;
  120. }
  121. }
  122. else
  123. cout << "Username Incorrect";
  124.  
  125. }
  126. else
  127. addUser_();
  128.  
  129. //keep program running
  130. cout << endl << "Run again? (Y/N) ";
  131. char V;
  132. cin >> V;
  133. if (V == 'N')
  134. run = false;
  135.  
  136. }
  137. }
  138.  
  139.  
  140.  
  141. void addUser_() {
  142. sql::Driver *driver;
  143. sql::Connection *con;
  144. driver = get_driver_instance();
  145. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  146. con->setSchema("ajacob18");
  147.  
  148. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  149. string username, password, first_name, last_name, email;
  150. cout << "Insert username, password, first name, last name and email: ";
  151. cin >> username >> password >> first_name >> last_name >> email;
  152. cout << endl;
  153.  
  154. vector<string> username_vector;
  155. username_vector.push_back(username);
  156. username_vector.push_back(password);
  157. username_vector.push_back(first_name);
  158. username_vector.push_back(last_name);
  159. username_vector.push_back(email);
  160.  
  161. pstmtptr.reset(con->prepareStatement("CALL add_username(?,?,?,?,?)"));
  162.  
  163. pstmtptr->setString(1, username_vector[0]);
  164. pstmtptr->setString(2, username_vector[1]);
  165. pstmtptr->setString(3, username_vector[2]);
  166. pstmtptr->setString(4, username_vector[3]);
  167. pstmtptr->setString(5, username_vector[4]);
  168. pstmtptr->execute();
  169.  
  170. delete con;
  171. pstmtptr.reset();
  172. };
  173.  
  174. bool verifyExistence_(string username) {
  175. sql::Driver *driver;
  176. sql::Connection *con;
  177. driver = get_driver_instance();
  178. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  179. con->setSchema("ajacob18");
  180. std::auto_ptr<sql::Statement> stmt(con->createStatement());
  181. std::auto_ptr< sql::PreparedStatement > pstmtptr;
  182.  
  183. pstmtptr.reset(con->prepareStatement("CALL username_exists(?, @count)"));
  184. pstmtptr->setString(1, username);
  185. pstmtptr->execute();
  186.  
  187. std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @count AS username_count"));
  188. if (res->next()){
  189. if (res->getInt("username_count") == 0)
  190. return false;
  191. else
  192. return true;
  193. }
  194. delete con;
  195. pstmtptr.reset();
  196. };
  197.  
  198. void vote_(string votee) {
  199. sql::Driver *driver;
  200. sql::Connection *con;
  201. sql::ResultSet *res;
  202. driver = get_driver_instance();
  203. con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
  204. con->setSchema("ajacob18");
  205.  
  206. std::auto_ptr< sql::PreparedStatement > pstmt;
  207. sql::Statement *stmt;
  208.  
  209. pstmt.reset(con->prepareStatement("CALL vote(?)"));
  210. pstmt->setString(1, votee);
  211. pstmt->execute();
  212.  
  213. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement