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_();
- bool verifyExistence_(string);
- void vote_(string);
- 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;
- if (answer == "Y") {
- cout << endl << "Enter username: ";
- cin >> username;
- if (verifyExistence_(username) == true) {
- cout << endl << "Enter Password: ";
- cin >> password;
- 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 << "Username Incorrect";
- }
- else
- addUser_();
- //keep program running
- cout << endl << "Run again? (Y/N) ";
- char V;
- cin >> V;
- if (V == 'N')
- run = false;
- }
- }
- 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();
- };
- 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();
- };
- 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