Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //VLR cpp with main
- /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
- There are special exceptions to the terms and conditions of the GPL
- as it is applied to this software. View the full text of the
- exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
- software distribution.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /* @README A
- - Connect to dbdev.cs.kent.edu server using your flashline username and password
- - place this file (CompleteExample2.cpp) on your home directory or
- anywhere, this is only for testing purposes
- */
- #include <stdlib.h>
- #include <iostream>
- #include <string>
- #include <vector>
- /*
- Include directly the different
- headers from cppconn/ and mysql_driver.h + mysql_util.h
- (and mysql_connection.h). This will reduce your build time!
- */
- #include "mysql_connection.h"
- #include <cppconn/driver.h>
- #include <cppconn/exception.h>
- #include <cppconn/resultset.h>
- #include <cppconn/statement.h>
- #include <cppconn/prepared_statement.h>
- using namespace std;
- void addUser_();
- void addCandidate_();
- void dropUser_();
- void dropCandidate_();
- bool verifyExistence_(string);
- void vote_(string);
- bool verifyPassword_(string, string);
- void displayVote_();
- int main() {
- sql::Driver *driver;
- sql::Connection *con;
- sql::Statement *stmt;
- sql::ResultSet *res;
- sql::PreparedStatement *pstmt;
- std::auto_ptr< sql::PreparedStatement > pstmtptr;
- //sql::ResultSet *res2;
- //sql::Statement *stmt2;
- bool run = true;
- bool voteAgain = true;
- std::vector<string> voteConstraint_;
- voteConstraint_.push_back("setUp");
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT * from candidate");
- while (run == true) {
- cout << "Do you have an account? (Y/N) ";
- string answer, username, password, voteAgainString;
- cin >> answer;
- //Asking if currently have user
- if (answer == "Y") {
- cout << endl << "Enter username: ";
- cin >> username;
- //Admin user
- bool continue_ = true;
- if (username == "admin") {
- string adminPassword, more;
- cout << "Enter admin password(password is password): ";
- cin >> adminPassword;
- if (adminPassword == "password") {
- int choice;
- while (continue_ == true) {
- cout << "What would you like to do?" << endl;
- cout << "Add User(1), Add Candidate(2), Drop User(3), Drop Candidate(4), Display Votes(5)" << endl;
- cin >> choice;
- switch(choice) {
- case 1: {addUser_(); break;}
- case 2: {addCandidate_(); break;}
- case 3: {dropUser_(); break;}
- case 4: {dropCandidate_(); break;}
- case 5: {displayVote_(); break;}
- default: {cout << "Did not enter a choice"; break;}
- }
- cout << "Would you like to do more?(Y/N) ";
- cin >> more;
- if (more == "N")
- continue_ = false;
- }
- }
- }
- //verifying username
- if (verifyExistence_(username) == true && username != "admin") {
- cout << endl << "Enter Password: ";
- cin >> password;
- //verifying password
- if (verifyPassword_(username, password) == true) {
- while (voteAgain == true) {
- //stmt = con->createStatement();
- //res = stmt->executeQuery("SELECT * from candidate");
- while (res->next()) {
- cout << res->getString("username") << endl;
- }
- cout << endl << "Enter username to vote: ";
- cin >> username;
- //give vector a length
- //voteConstraint_.push_back("setUp");
- for (int i = 0; i < voteConstraint_.size(); i++) {
- cout << voteConstraint_.size() << endl;
- if (username == voteConstraint_[i]) {
- cout << endl << "Already voted for!";
- voteAgain = false;
- }
- }
- cout << voteConstraint_.size() << endl;
- if (voteAgain == true) {
- voteConstraint_.push_back(username);
- vote_(username);
- }
- //voteConstraint_.push_back(username);
- cout << "Would you like to vote again?";
- cin >> voteAgainString;
- if (voteAgainString == "N")
- voteAgain = false;
- }
- }
- else
- cout << "Incorrect password!" << endl;
- }
- else
- cout << "Username Incorrect";
- }
- else
- addUser_();
- //keep program running
- cout << endl << "Run again? (Y/N) ";
- char V;
- cin >> V;
- if (V == 'N')
- run = false;
- }
- }
- void displayVote_() {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- sql::Statement *stmt;
- sql::ResultSet *res;
- std::auto_ptr< sql::PreparedStatement > pstmt;
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT * from vote");
- while (res->next()) {
- cout << res->getString("username") << " " << res->getInt("vote") << endl;
- }
- };
- void addUser_() {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- std::auto_ptr< sql::PreparedStatement > pstmtptr;
- string username, password, first_name, last_name, email;
- cout << "Insert username, password, first name, last name and email: ";
- cin >> username >> password >> first_name >> last_name >> email;
- cout << endl;
- vector<string> username_vector;
- username_vector.push_back(username);
- username_vector.push_back(password);
- username_vector.push_back(first_name);
- username_vector.push_back(last_name);
- username_vector.push_back(email);
- pstmtptr.reset(con->prepareStatement("CALL add_username(?,?,?,?,?)"));
- pstmtptr->setString(1, username_vector[0]);
- pstmtptr->setString(2, username_vector[1]);
- pstmtptr->setString(3, username_vector[2]);
- pstmtptr->setString(4, username_vector[3]);
- pstmtptr->setString(5, username_vector[4]);
- pstmtptr->execute();
- delete con;
- pstmtptr.reset();
- };
- void addCandidate_() {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- sql::Statement *stmt;
- sql::ResultSet *res;
- std::auto_ptr< sql::PreparedStatement > pstmt;
- string votee;
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT * from user");
- while (res->next()) {
- cout << res->getString("username") << endl;
- }
- cout << endl << "Who do you want to add? " << endl;
- cin >> votee;
- pstmt.reset(con->prepareStatement("CALL add_candidate(?)"));
- pstmt->setString(1, votee);
- pstmt->execute();
- };
- void dropUser_() {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- sql::Statement *stmt;
- sql::ResultSet *res;
- std::auto_ptr< sql::PreparedStatement > pstmt;
- string votee;
- cout << "Who do you want to add? " << endl;
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT * from user");
- while (res->next()) {
- cout << res->getString("username") << endl;
- }
- cin >> votee;
- pstmt.reset(con->prepareStatement("CALL drop_user(?)"));
- pstmt->setString(1, votee);
- pstmt->execute();
- };
- void dropCandidate_() {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- sql::Statement *stmt;
- sql::ResultSet *res;
- std::auto_ptr< sql::PreparedStatement > pstmt;
- string votee;
- cout << "Who do you want to delete? " << endl;
- stmt = con->createStatement();
- res = stmt->executeQuery("SELECT * from candidate");
- while (res->next()) {
- cout << res->getString("username") << endl;
- }
- cin >> votee;
- cout << endl;
- pstmt.reset(con->prepareStatement("CALL drop_candidate(?)"));
- pstmt->setString(1, votee);
- pstmt->execute();
- };
- bool verifyExistence_(string username) {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- std::auto_ptr<sql::Statement> stmt(con->createStatement());
- std::auto_ptr< sql::PreparedStatement > pstmtptr;
- pstmtptr.reset(con->prepareStatement("CALL username_exists(?, @count)"));
- pstmtptr->setString(1, username);
- pstmtptr->execute();
- std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @count AS username_count"));
- if (res->next()){
- if (res->getInt("username_count") == 0)
- return false;
- else
- return true;
- }
- delete con;
- pstmtptr.reset();
- };
- bool verifyPassword_(string username, string password) {
- sql::Driver *driver;
- sql::Connection *con;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- std::auto_ptr<sql::Statement> stmt(con->createStatement());
- std::auto_ptr< sql::PreparedStatement > pstmtptr;
- pstmtptr.reset(con->prepareStatement("CALL password_correct(?, ?, @count)"));
- pstmtptr->setString(1, username);
- pstmtptr->setString(2, password);
- pstmtptr->execute();
- std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @count AS password_count"));
- if (res->next()){
- if (res->getInt("password_count") == 1)
- return true;
- else
- return false;
- }
- delete con;
- pstmtptr.reset();
- };
- void vote_(string votee) {
- sql::Driver *driver;
- sql::Connection *con;
- sql::ResultSet *res;
- driver = get_driver_instance();
- con = driver->connect("dbdev.cs.kent.edu", "ajacob18", "En5Rqsf1");
- con->setSchema("ajacob18");
- std::auto_ptr< sql::PreparedStatement > pstmt;
- sql::Statement *stmt;
- pstmt.reset(con->prepareStatement("CALL vote(?)"));
- pstmt->setString(1, votee);
- pstmt->execute();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement