From 45b4c5d6e0cc8f6099cd9958f49f86123967a4c8 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Mon, 10 Jun 2013 22:55:53 -0700 Subject: [PATCH 01/13] made a simple comment saying lololol --- EmbeddedSQL.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index 65c9890..b2d60e9 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -12,7 +12,7 @@ * Target DBMS: 'Postgres' * */ - +//LOLLOL CHANGE import java.sql.DriverManager; import java.sql.Connection; From 0175603c1055d251002e7ecbced77eea176c05e7 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Mon, 10 Jun 2013 23:43:03 -0700 Subject: [PATCH 02/13] removed stuff --- EmbeddedSQL.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index b2d60e9..65c9890 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -12,7 +12,7 @@ * Target DBMS: 'Postgres' * */ -//LOLLOL CHANGE + import java.sql.DriverManager; import java.sql.Connection; From 2e8fdbb70cffc0cfb60c856d93694ceb498d13ba Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Mon, 10 Jun 2013 23:49:19 -0700 Subject: [PATCH 03/13] asdada --- EmbeddedSQL.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index ea87599..b536355 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -14,7 +14,7 @@ */ //FUCK THIS TESTING - +//asdadadsadsad import java.sql.DriverManager; import java.sql.Connection; From 40cc9309e5d1d81af3eebe4491355bfc691f6dff Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 01:26:19 -0700 Subject: [PATCH 04/13] Added Register Users --- EmbeddedSQL.java | 103 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 24 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index b536355..d62f276 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -12,9 +12,6 @@ * Target DBMS: 'Postgres' * */ - - //FUCK THIS TESTING -//asdadadsadsad import java.sql.DriverManager; import java.sql.Connection; @@ -89,7 +86,7 @@ public void executeUpdate (String sql) throws SQLException { stmt.close (); }//end executeUpdate - public String executeLoginQuery (String query) throws SQLException{ + public int executeLoginQuery (String query) throws SQLException{ // creates a statement objectv Statement stmt = this._connection.createStatement (); @@ -103,7 +100,7 @@ public String executeLoginQuery (String query) throws SQLException{ ResultSetMetaData rsmd = rs.getMetaData (); int numCol = rsmd.getColumnCount (); rs.next(); - String resultset = rs.getString(numCol); + int resultset = Integer.parseInt(rs.getString(numCol)); System.out.println(resultset); stmt.close(); @@ -223,7 +220,9 @@ public static void main (String[] args) { switch (readChoice()){ case 0: LogInQuery(esql); break; - case 1: RegisterQuery(esql); break; + ///////////////////////////////////////////////////////////// + case 1: System.out.println(RegisterQuery(esql)); break; + ///////////////////////////////////////////////////////////// case 9: keepon = false; break; default : System.out.println("Unrecognized choice!"); break; }//end switch @@ -280,22 +279,23 @@ public static void LogInQuery(EmbeddedSQL esql){ String query = "SELECT COUNT(user_id) FROM users WHERE user_id='"; query += input + "'"; - String output = esql.executeLoginQuery (query); - if( output != "1" ) { + int output = esql.executeLoginQuery (query); + System.out.println("This is output" + output); + if( output != 1 ) { System.out.println("Sorry, that username isn't in our records. Please try again."); continue; } - else if( output == "1" ){ + else if( output == 1 ){ currentUser = input; System.out.println("Please specify your password:"); input = in.readLine(); query = "SELECT COUNT(user_id) FROM users WHERE user_id='" + currentUser + "' AND password='" + input + "'"; - String output2 = esql.executeLoginQuery(query); - if( output2 != "1" ){ + int output2 = esql.executeLoginQuery(query); + if( output2 != 1 ){ System.out.println("Sorry, the username and password don't match up. Please try again."); continue; } - else if( output2 == "1" ){ + else if( output2 == 1 ){ System.out.println("Successfully logged in!"); break; } @@ -308,18 +308,73 @@ else if( output2 == "1" ){ System.err.println (e.getMessage ()); } }//end QueryExample - - public static void RegisterQuery(EmbeddedSQL esql){ - try{ - String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " - + "WHERE C.sid=S.sid GROUP BY C.sid;"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query1 + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + public static int RegisterQuery(EmbeddedSQL esql){ + System.out.println("You have selected to register yourself as a user. Please specify the following information."); + try{ + System.out.println("Enter a username you want (Username must be 9 chars or less long):"); + String input = in.readLine(); + + //if( input.length() > 9 ) { System.out.println("Invalid Username (more than 9 chars)"); continue; } + + String query = "INSERT INTO Users (user_id, password, first_name, middle_name, last_name, e_mail, street1, street2, state, country, zipcode, balance)" + + "VALUES ('" + input + "','"; + + System.out.println("Enter your password:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your first name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your middle name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your last name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your email:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your street1:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your street2:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your state:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your country:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your zipcode:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your balance:"); + input = in.readLine(); + query += input + "')"; + + esql.executeUpdate (query); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + return 1; + }//end catch + return 0; + }//end RegisterQuery + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void Query2(EmbeddedSQL esql){ try{ From 0a3ffa2d968138b130a6864dbf2ec1952a476e9a Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 04:21:14 -0700 Subject: [PATCH 05/13] Added the SuperUser and Register Movie implementation kinda --- EmbeddedSQL.java | 293 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 278 insertions(+), 15 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index d62f276..9d44fde 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -13,6 +13,7 @@ * */ + import java.sql.DriverManager; import java.sql.Connection; import java.sql.Statement; @@ -24,6 +25,12 @@ import java.io.BufferedReader; import java.io.InputStreamReader; + +/////////////////////////////////////////////////////// +//////////////////////////////////////////////////// +///////// ADD THIS IMPORT //////////////// +import java.util.Scanner; + /** * This class defines a simple embedded SQL utility class that is designed to * work with PostgreSQL JDBC drivers. @@ -32,7 +39,8 @@ public class EmbeddedSQL { - public static String currentUser; + public static String currentUser; + public static boolean superUser = false; // reference to physical database connection. private Connection _connection = null; @@ -85,6 +93,67 @@ public void executeUpdate (String sql) throws SQLException { // close the instruction stmt.close (); }//end executeUpdate + + public String executeStringQuery (String query) throws SQLException + { + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery (query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + System.out.println("--------------"); + while(rs.next()) + { + System.out.println(rs.getString(1)); + } + + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + String resultset = rs.getString(numCol); + System.out.println(resultset); + + stmt.close(); + return resultset; + } + + public void executeOrderQuery (String query) throws SQLException + { + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery(query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + + System.out.println("--------------"); + + while(rs.next()) + { + System.out.println("You are about to order:"); + System.out.println(rs.getString(1)); + } +/* + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + String resultset = rs.getString(numCol); + System.out.println(resultset); + + stmt.close(); + return resultset; +*/ + } + public int executeLoginQuery (String query) throws SQLException{ // creates a statement objectv @@ -217,16 +286,59 @@ public static void main (String[] args) { System.out.println("0. Yes I am!"); System.out.println("1. No, I need to register!"); System.out.println("9. < EXIT (Stop the program)"); + switch (readChoice()){ - case 0: LogInQuery(esql); break; - ///////////////////////////////////////////////////////////// - case 1: System.out.println(RegisterQuery(esql)); break; - ///////////////////////////////////////////////////////////// - case 9: keepon = false; break; + case 0: if(LogInQuery(esql) == 0 ) keepon = false; break; + case 1: if(RegisterQuery(esql) == 0 ) keepon = false; break; + case 9: return; default : System.out.println("Unrecognized choice!"); break; }//end switch - }//end while + }//end while + + //Will need to turn keepon to false after logging in + + boolean homescreen = true; + while(homescreen) + { + System.out.println("Welcome to MovieNet"); + System.out.println("---------"); + System.out.println("0. See list of movies WORKING"); + System.out.println("1. Order a movie NOT WORKING"); + System.out.println("2. Rate a movie NOT WORKING"); + System.out.println("3. Look at wall NOT WORKING"); + + if( superUser ){ + System.out.println("\nSuper User Options:"); + System.out.println("5. Register a new movie"); + System.out.println("6. Delete an existing movie"); + System.out.println("7. Delete an existing user"); + } + + /////////////////////////////////////////////////////////////CHANGES////////// + if( !superUser ) System.out.println("8. Login as SuperUser"); + /////////////////////////////////////////////////////////////CHANGES////////// + System.out.println("9. < EXIT (Stop the program)"); + + switch (readChoice()){ + case 0: ListMovies(esql); break; + case 1: OrderMovie(esql); break; + + /////////////////////////////////////////////////////////////CHANGES////////// + case 5: if( superUser ) {RegisterMovie(esql);}; break; + case 6: break; + case 7: break; + case 8: if( !superUser ) {LoginAsSuper(esql);}; break; + /////////////////////////////////////////////////////////////CHANGES////////// + + case 9: return; + default : System.out.println("Unrecognized choice!"); break; + } + + + } + + }catch(Exception e) { System.err.println (e.getMessage ()); }finally{ @@ -243,13 +355,149 @@ public static void main (String[] args) { }//end try }//end main - public static void Greeting(){ + + + public static void ListMovies(EmbeddedSQL esql) + { + try{ + + String query = "SELECT title FROM Video"; + String output = esql.executeStringQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } + + public static void OrderMovie(EmbeddedSQL esql) + { + try{ + + Scanner user_input = new Scanner(System.in); + + System.out.print("Title of movie you would like to order: "); + + String query = "SELECT video_id, title FROM video WHERE title = "; + + + boolean flag = true; + while(flag) + { + String title = in.readLine(); + + if(title != null) + flag = false; + } + + + + esql.executeOrderQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } + + + + // NOT FULLY IMPLEMENTED NEED TO GRAB VIDEO ID AND THEN RATE MOVIE + public static void RateMovie(EmbeddedSQL esql) + { + try{ + + System.out.print("Title of movie you would like to rate: "); + String title = in.readLine(); + System.out.print("What would you like to rate the movie (0 - 10): "); + String rating = in.readLine(); + + String query = "SELECT title FROM Video"; + + String output = esql.executeStringQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } + + public static void Greeting(){ System.out.println( "\n\n*******************************************************\n" + " User Interface \n" + "*******************************************************\n"); }//end Greeting - + + ///////////////////////////////////////////////////////////////START////////// + /////////////////////////////////////////////////////////////CHANGES////////// + public static int LoginAsSuper(EmbeddedSQL esql) + { + try{ + System.out.println("Verifying that you are a Super User"); + String query = "SELECT COUNT(super_user_id) FROM super_user WHERE super_user_id='" + currentUser + "';"; + + int resultset = esql.executeLoginQuery(query); + + if( resultset != 1 ){ + System.out.println("I'm sorry but you are not a registered super user"); + return 1; + } + + else if( resultset == 1 ){ + System.out.println("Welcome super user " + currentUser); + superUser = true; + return 0; + } + + }catch(Exception e){ + System.err.println( e.getMessage() ); + return 1; + } + return 0; + }//end LoginAsSuper + + + public static int RegisterMovie(EmbeddedSQL esql) + { + try{ + boolean isSeries = false; + System.out.println("Please enter the following information to register a new movie/series"); + System.out.println("Is this video part of a series (y/n)?"); + String input = in.readLine(); + if( input.equals("y") ) isSeries = true; + + String query = "SELECT MAX(video_id) FROM video;"; + Statement stmt = esql._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery (query); + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + int resultset = Integer.parseInt(rs.getString(numCol)) + 1; + + query = "INSERT INTO video( video_id, title, year, online_price, dvd_price"; + + + }catch(Exception e){ + System.err.println( e.getMessage() ); + return 1; + } + return 0; + } + + + /////////////////////////////////////////////////////////////CHANGES////////// + /////////////////////////////////////////////////////////////CHANGES////////// + //////////////////////////////////////////////////////////////END////////////// /* * Reads the users choice given from the keyboard * @int @@ -270,7 +518,7 @@ public static int readChoice() { return input; }//end readChoice - public static void LogInQuery(EmbeddedSQL esql){ + public static int LogInQuery(EmbeddedSQL esql){ try{ System.out.println("You are trying to Login as an existing user, enter your Username (Make sure it is only 9 chars long)"); while( true ){ @@ -302,13 +550,15 @@ else if( output2 == 1 ){ } } - //System.out.println ("total row(s): " + rowCount); }catch(Exception e){ - System.err.println (e.getMessage ()); - } + System.err.println (e.getMessage ()); + return 1; + } + return 0; }//end QueryExample - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static int RegisterQuery(EmbeddedSQL esql){ System.out.println("You have selected to register yourself as a user. Please specify the following information."); @@ -376,6 +626,19 @@ public static int RegisterQuery(EmbeddedSQL esql){ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/* + public static void RegisterQuery(EmbeddedSQL esql){ + try{ + String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " + + "WHERE C.sid=S.sid GROUP BY C.sid;"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query1 + public static void Query2(EmbeddedSQL esql){ try{ String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " @@ -456,5 +719,5 @@ public static void Query6(EmbeddedSQL esql){ System.err.println (e.getMessage ()); } }//end Query6 - +*/ }//end EmbeddedSQL From 898434216251ee564dc3fbf25f222c54c9d4a51f Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 07:16:29 -0700 Subject: [PATCH 06/13] Added more code to register video --- EmbeddedSQL.java | 108 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 13 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index 9d44fde..ba1bcc6 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -466,26 +466,108 @@ public static int RegisterMovie(EmbeddedSQL esql) try{ boolean isSeries = false; System.out.println("Please enter the following information to register a new movie/series"); + + System.out.println("What is the title of this video?"); + inputTitle = in.readLine(); + System.out.println("Is this video part of a series (y/n)?"); - String input = in.readLine(); - if( input.equals("y") ) isSeries = true; + String inputSeries = in.readLine(); + String inputTitle = ""; + String inputYear = ""; + int inputOnlinePrice; + int inputDvdPrice; + int inputRating; + String inputSeasonNum = ""; + String inputEpNum = ""; + int series_id; + int season_id; + if( inputSeries.equals("y") ){ + isSeries = true; + + System.out.println("What season number is this?"); + inputSeasonNum = in.readLine(); + + System.out.println("What episode is this?"); + inputEpNum = in.readLine(); + } + + System.out.println("What year did this come out?"); + inputYear = in.readLine(); + + System.out.println("How much should the online price be?"); + inputOnlinePrice = Integer.parseInt(in.readLine()); + System.out.println("How much should the dvd price be?"); + inputDvdPrice = Integer.parseInt(in.readLine()); + + System.out.println("How would you rate this (0-10)?"); + inputRating = Integer.parseInt(in.readLine()); + + //this will get the video id String query = "SELECT MAX(video_id) FROM video;"; Statement stmt = esql._connection.createStatement (); - - // issues the query instruction - ResultSet rs = stmt.executeQuery (query); - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); + ResultSet rs = stmt.executeQuery (query); + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); rs.next(); - int resultset = Integer.parseInt(rs.getString(numCol)) + 1; + int video_id = Integer.parseInt(rs.getString(numCol)) + 1; + System.out.println(video_id); + stmt.close(); - query = "INSERT INTO video( video_id, title, year, online_price, dvd_price"; + //this will check if the series is already in record if not then it adds it + if( isSeries ){ + stmt = esql._connection.createStatement(); + String getSeriesId = "SELECT series_id FROM series WHERE title='" + inputTitle + "'"; + rs = stmt.executeQuery (getSeriesId); + rsmd = rs.getMetaData (); + numCol = rsmd.getColumnCount (); + + //get the series id + if(rs.next()) series_id = Integer.parseInt(rs.getString(numCol)); + else{ + getSeriesId = "SELECT MAX(series_id) FROM series;"; + rs = stmt.executeQuery(getSeriesId); + rsmd = rs.getMetaData(); + if(rs.next()) series_id = Integer.parseInt(rs.getString(1)) + 1; + else series_id = 1; + + //add series information to database + String insertSeries = "INSERT INTO series (series_id, title) VALUES (" + series_id + ", '" + inputTitle + "');"; + esql.executeUpdate(insertSeries); + } + String getSeasonId = "SELECT season_id FROM season WHERE series_id='" + series_id + "' AND season_number='" + inputSeasonNum + "';"; + rs = stmt.executeQuery(getSeasonId); + rsmd = rs.getMetaData(); + numCol = rsmd.getColumnCount(); + + //if the season id already exists grab the season id + if(rs.next()) season_id = Integer.parseInt(rs.getString(1)); + else{ + getSeasonId = "SELECT MAX(season_id) FROM season;"; + rs = stmt.executeQuery(getSeasonId); + rsmd = rs.getMetaData(); + if(rs.next()) season_id = Integer.parseInt(rs.getString(1)) + 1; + else season_id = 1; + + String insertSeason = "INSERT INTO season (season_id, series_id, season_number ) VALUES (" + season_id + ", " + series_id + ", '" + inputSeasonNum + "');"; + esql.executeUpdate(insertSeason); + } + + + //System.out.println("season_id " + season_id + " series_id " + series_id); + + String insertVideo = "INSERT INTO video (video_id, title, year, online_price, dvd_price, rating, episode, season_id) VALUES " + + "(" + video_id + ",'" + inputTitle + "', '" + inputYear + "', " + inputOnlinePrice + ", " + inputDvdPrice + ", " + inputRating + ", " + inputEpNum + ", " + season_id + ");"; + + esql.executeUpdate(insertVideo); + //if season doesnt exist already + //if( season_id == null ); + + System.out.println("SEASON_ID " + series_id); + + //query = "INSERT INTO video( video_id, title, year, online_price, dvd_price, "; + } }catch(Exception e){ System.err.println( e.getMessage() ); From 5c699ec10b2cc38e9a34ada53f4ef982ffa5d186 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 13:48:23 -0700 Subject: [PATCH 07/13] Finished Register video function and made a few minor changes, will merge with thomas in a bit --- EmbeddedSQL.java | 120 +++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 51 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index ba1bcc6..ec18bea 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -430,7 +430,7 @@ public static void Greeting(){ "\n\n*******************************************************\n" + " User Interface \n" + "*******************************************************\n"); - }//end Greeting + }//end Greeting ///////////////////////////////////////////////////////////////START////////// /////////////////////////////////////////////////////////////CHANGES////////// @@ -457,10 +457,10 @@ else if( resultset == 1 ){ System.err.println( e.getMessage() ); return 1; } - return 0; + return 0; }//end LoginAsSuper - + //Register a new video, creating new video_id, season_id, and series_id if necessary public static int RegisterMovie(EmbeddedSQL esql) { try{ @@ -468,19 +468,21 @@ public static int RegisterMovie(EmbeddedSQL esql) System.out.println("Please enter the following information to register a new movie/series"); System.out.println("What is the title of this video?"); - inputTitle = in.readLine(); + String inputTitle = in.readLine(); System.out.println("Is this video part of a series (y/n)?"); String inputSeries = in.readLine(); - String inputTitle = ""; + + String inputSeasonNum = ""; + String inputEpNum = ""; String inputYear = ""; int inputOnlinePrice; int inputDvdPrice; int inputRating; - String inputSeasonNum = ""; - String inputEpNum = ""; int series_id; int season_id; + + //if this is a season get season info if( inputSeries.equals("y") ){ isSeries = true; @@ -522,8 +524,9 @@ public static int RegisterMovie(EmbeddedSQL esql) rsmd = rs.getMetaData (); numCol = rsmd.getColumnCount (); - //get the series id + //get the series id if it already exists if(rs.next()) series_id = Integer.parseInt(rs.getString(numCol)); + //if series id doesnt already exist then create a new series value else{ getSeriesId = "SELECT MAX(series_id) FROM series;"; rs = stmt.executeQuery(getSeriesId); @@ -543,6 +546,7 @@ public static int RegisterMovie(EmbeddedSQL esql) //if the season id already exists grab the season id if(rs.next()) season_id = Integer.parseInt(rs.getString(1)); + //if the season id doesn't exist then place insert new season value else{ getSeasonId = "SELECT MAX(season_id) FROM season;"; rs = stmt.executeQuery(getSeasonId); @@ -554,20 +558,23 @@ public static int RegisterMovie(EmbeddedSQL esql) esql.executeUpdate(insertSeason); } - - //System.out.println("season_id " + season_id + " series_id " + series_id); - String insertVideo = "INSERT INTO video (video_id, title, year, online_price, dvd_price, rating, episode, season_id) VALUES " + "(" + video_id + ",'" + inputTitle + "', '" + inputYear + "', " + inputOnlinePrice + ", " + inputDvdPrice + ", " + inputRating + ", " + inputEpNum + ", " + season_id + ");"; esql.executeUpdate(insertVideo); - //if season doesnt exist already - //if( season_id == null ); System.out.println("SEASON_ID " + series_id); + stmt.close(); + }//End if video is a series - //query = "INSERT INTO video( video_id, title, year, online_price, dvd_price, "; - } + else if( !isSeries ) + { + String insertVideo = "INSERT INTO video (video_id, title, year, online_price, dvd_price, rating) VALUES " + + "(" + video_id + ",'" + inputTitle + "', '" + inputYear + "', " + inputOnlinePrice + ", " + inputDvdPrice + ", " + inputRating + ");"; + stmt = esql._connection.createStatement (); + esql.executeUpdate(insertVideo); + stmt.close(); + }//End if video is not series }catch(Exception e){ System.err.println( e.getMessage() ); @@ -599,52 +606,63 @@ public static int readChoice() { }while (true); return input; }//end readChoice - + public static int LogInQuery(EmbeddedSQL esql){ - try{ - System.out.println("You are trying to Login as an existing user, enter your Username (Make sure it is only 9 chars long)"); - while( true ){ - System.out.println("Please specify your username:"); - String input = in.readLine(); - String query = "SELECT COUNT(user_id) FROM users WHERE user_id='"; - query += input + "'"; - + try{ + System.out.println("You are trying to Login as an existing user, enter your Username (Make sure it is only 9 chars long) or press 9 to go back"); + //Keep asking the user to login if they want to log in + while( true ){ + System.out.println("Please specify your username or press 9 to go back:"); + String inputUser = in.readLine(); + + if( inputUser.equals("9") ) return 1; //exit the loop and go back to home screen + + String query = "SELECT COUNT(user_id) FROM users WHERE user_id='"; + query += inputUser + "'"; + int output = esql.executeLoginQuery (query); System.out.println("This is output" + output); - if( output != 1 ) { - System.out.println("Sorry, that username isn't in our records. Please try again."); - continue; - } - else if( output == 1 ){ - currentUser = input; - System.out.println("Please specify your password:"); - input = in.readLine(); - query = "SELECT COUNT(user_id) FROM users WHERE user_id='" + currentUser + "' AND password='" + input + "'"; - int output2 = esql.executeLoginQuery(query); - if( output2 != 1 ){ - System.out.println("Sorry, the username and password don't match up. Please try again."); - continue; - } - else if( output2 == 1 ){ - System.out.println("Successfully logged in!"); - break; - } - } - } - - //System.out.println ("total row(s): " + rowCount); - }catch(Exception e){ + + //check if username is valid, if not 1 then either output = 0 or > 2 which is a problem + if( output != 1 ) { + System.out.println("Sorry, that username isn't in our records. Please try again."); + continue; + }//end username not found + + //if output is 1 then only 1 record with that user found (good) + else if( output == 1 ){ + System.out.println("Please specify your password:"); + String inputPass = in.readLine(); + query = "SELECT COUNT(user_id) FROM users WHERE user_id='" + inputUser + "' AND password='" + inputPass + "'"; + + int output2 = esql.executeLoginQuery(query); //returns number of query occurances + + //if password doesn't match records ask for username again + if( output2 != 1 ){ + System.out.println("Sorry, the username and password don't match up. Please try again."); + continue; + }//end password fail + + //if password correct then save current user info and log them in + else if( output2 == 1 ){ + currentUser = inputUser; + System.out.println("Successfully logged in! Welcome " + currentUser + "!"); + break; + }//end password success + }//end username found + }//end user login prompt + }catch(Exception e){ System.err.println (e.getMessage ()); return 1; - } + }//end catch return 0; - }//end QueryExample - + }//end LoginQuery + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static int RegisterQuery(EmbeddedSQL esql){ System.out.println("You have selected to register yourself as a user. Please specify the following information."); - try{ + try{ System.out.println("Enter a username you want (Username must be 9 chars or less long):"); String input = in.readLine(); From 4ac19af295803461ec65842bbc966e276f952614 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 13:51:40 -0700 Subject: [PATCH 08/13] removed all the dam ^M characters --- EmbeddedSQL.java | 1156 +++++++++++++++++++++++----------------------- 1 file changed, 578 insertions(+), 578 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index ec18bea..fcf6775 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -1,299 +1,299 @@ -/* - * MODIFIED BY JUSTIN KAHAL AND THOMAS DESMOND - * SSID:860892022 - * - * Template JAVA User Interface - * ============================= - * - * Database Management Systems - * Department of Computer Science & Engineering - * University of California - Riverside - * - * Target DBMS: 'Postgres' - * - */ - - -import java.sql.DriverManager; -import java.sql.Connection; -import java.sql.Statement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.io.File; -import java.io.FileReader; -import java.io.BufferedReader; -import java.io.InputStreamReader; - - -/////////////////////////////////////////////////////// -//////////////////////////////////////////////////// -///////// ADD THIS IMPORT //////////////// -import java.util.Scanner; - -/** - * This class defines a simple embedded SQL utility class that is designed to - * work with PostgreSQL JDBC drivers. - * - */ -public class EmbeddedSQL { - - +/* + * MODIFIED BY JUSTIN KAHAL AND THOMAS DESMOND + * SSID:860892022 + * + * Template JAVA User Interface + * ============================= + * + * Database Management Systems + * Department of Computer Science & Engineering + * University of California - Riverside + * + * Target DBMS: 'Postgres' + * + */ + + +import java.sql.DriverManager; +import java.sql.Connection; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.io.File; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.InputStreamReader; + + +/////////////////////////////////////////////////////// +//////////////////////////////////////////////////// +///////// ADD THIS IMPORT //////////////// +import java.util.Scanner; + +/** + * This class defines a simple embedded SQL utility class that is designed to + * work with PostgreSQL JDBC drivers. + * + */ +public class EmbeddedSQL { + + public static String currentUser; public static boolean superUser = false; - // reference to physical database connection. - private Connection _connection = null; - - // handling the keyboard inputs through a BufferedReader - // This variable can be global for convenience. - static BufferedReader in = new BufferedReader( - new InputStreamReader(System.in)); - - /** - * Creates a new instance of EmbeddedSQL - * - * @param hostname the MySQL or PostgreSQL server hostname - * @param database the name of the database - * @param username the user name used to login to the database - * @param password the user login password - * @throws java.sql.SQLException when failed to make a connection. - */ - public EmbeddedSQL (String dbname, String dbport, String user, String passwd) throws SQLException { - - System.out.print("Connecting to database..."); - try{ - // constructs the connection URL - String url = "jdbc:postgresql://localhost:" + dbport + "/" + dbname; - System.out.println ("Connection URL: " + url + "\n"); - - // obtain a physical connection - this._connection = DriverManager.getConnection(url, user, passwd); - System.out.println("Done"); - }catch (Exception e){ - System.err.println("Error - Unable to Connect to Database: " + e.getMessage() ); - System.out.println("Make sure you started postgres on this machine"); - System.exit(-1); - }//end catch - }//end EmbeddedSQL - - /** - * Method to execute an update SQL statement. Update SQL instructions - * includes CREATE, INSERT, UPDATE, DELETE, and DROP. - * - * @param sql the input SQL string - * @throws java.sql.SQLException when update failed - */ - public void executeUpdate (String sql) throws SQLException { - // creates a statement object - Statement stmt = this._connection.createStatement (); - - // issues the update instruction - stmt.executeUpdate (sql); - - // close the instruction - stmt.close (); - }//end executeUpdate + // reference to physical database connection. + private Connection _connection = null; + + // handling the keyboard inputs through a BufferedReader + // This variable can be global for convenience. + static BufferedReader in = new BufferedReader( + new InputStreamReader(System.in)); + + /** + * Creates a new instance of EmbeddedSQL + * + * @param hostname the MySQL or PostgreSQL server hostname + * @param database the name of the database + * @param username the user name used to login to the database + * @param password the user login password + * @throws java.sql.SQLException when failed to make a connection. + */ + public EmbeddedSQL (String dbname, String dbport, String user, String passwd) throws SQLException { + + System.out.print("Connecting to database..."); + try{ + // constructs the connection URL + String url = "jdbc:postgresql://localhost:" + dbport + "/" + dbname; + System.out.println ("Connection URL: " + url + "\n"); + + // obtain a physical connection + this._connection = DriverManager.getConnection(url, user, passwd); + System.out.println("Done"); + }catch (Exception e){ + System.err.println("Error - Unable to Connect to Database: " + e.getMessage() ); + System.out.println("Make sure you started postgres on this machine"); + System.exit(-1); + }//end catch + }//end EmbeddedSQL + + /** + * Method to execute an update SQL statement. Update SQL instructions + * includes CREATE, INSERT, UPDATE, DELETE, and DROP. + * + * @param sql the input SQL string + * @throws java.sql.SQLException when update failed + */ + public void executeUpdate (String sql) throws SQLException { + // creates a statement object + Statement stmt = this._connection.createStatement (); + + // issues the update instruction + stmt.executeUpdate (sql); + + // close the instruction + stmt.close (); + }//end executeUpdate - public String executeStringQuery (String query) throws SQLException - { - // creates a statement objectv - Statement stmt = this._connection.createStatement (); - - // issues the query instruction - ResultSet rs = stmt.executeQuery (query); - - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - System.out.println("--------------"); - while(rs.next()) - { - System.out.println(rs.getString(1)); - } - - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - rs.next(); - String resultset = rs.getString(numCol); - System.out.println(resultset); - - stmt.close(); - return resultset; - } - - public void executeOrderQuery (String query) throws SQLException - { - // creates a statement objectv - Statement stmt = this._connection.createStatement (); - - // issues the query instruction - ResultSet rs = stmt.executeQuery(query); - - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - - System.out.println("--------------"); - - while(rs.next()) - { - System.out.println("You are about to order:"); - System.out.println(rs.getString(1)); - } -/* - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - rs.next(); - String resultset = rs.getString(numCol); - System.out.println(resultset); - - stmt.close(); - return resultset; -*/ - } - - - public int executeLoginQuery (String query) throws SQLException{ - // creates a statement objectv - Statement stmt = this._connection.createStatement (); - - // issues the query instruction - ResultSet rs = stmt.executeQuery (query); - - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - rs.next(); - int resultset = Integer.parseInt(rs.getString(numCol)); - System.out.println(resultset); - - stmt.close(); - return resultset; - // iterates through the result set and output them to standard out. - /* boolean outputHeader = true; - while (rs.next()){ - if(outputHeader){ - for(int i = 1; i <= numCol; i++){ - System.out.print(rsmd.getColumnName(i) + "\t"); - } - System.out.println(); - outputHeader = false; - } - for (int i=1; i<=numCol; ++i) - System.out.print (rs.getString (i) + "\t"); - System.out.println (); - ++rowCount; - }//end while - stmt.close (); - return rowCount; - */ - } - - /** - * Method to execute an input query SQL instruction (i.e. SELECT). This - * method issues the query to the DBMS and outputs the results to - * standard out. - * - * @param query the input query string - * @return the number of rows returned - * @throws java.sql.SQLException when failed to execute the query - */ - public int executeQuery (String query) throws SQLException { - // creates a statement objectv - Statement stmt = this._connection.createStatement (); - - // issues the query instruction - ResultSet rs = stmt.executeQuery (query); - - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - int rowCount = 0; - - // iterates through the result set and output them to standard out. - boolean outputHeader = true; - while (rs.next()){ - if(outputHeader){ - for(int i = 1; i <= numCol; i++){ - System.out.print(rsmd.getColumnName(i) + "\t"); - } - System.out.println(); - outputHeader = false; - } - for (int i=1; i<=numCol; ++i) - System.out.print (rs.getString (i) + "\t"); - System.out.println (); - ++rowCount; - }//end while - stmt.close (); - return rowCount; - }//end executeQuery - - /** - * Method to close the physical connection if it is open. - */ - public void cleanup(){ - try{ - if (this._connection != null){ - this._connection.close (); - }//end if - }catch (SQLException e){ - // ignored. - }//end try - }//end cleanup - - /** - * The main execution method - * - * @param args the command line arguments this inclues the - */ - public static void main (String[] args) { - if (args.length != 4) { - System.err.println ( - "Usage: " + - "java [-classpath ] " + - EmbeddedSQL.class.getName () + - " "); - return; - }//end if - - Greeting(); - EmbeddedSQL esql = null; - try{ - // use postgres JDBC driver. - Class.forName ("org.postgresql.Driver").newInstance (); - // instantiate the EmbeddedSQL object and creates a physical - // connection. - String dbname = args[0]; - String dbport = args[1]; - String user = args[2]; - String passwd = args[3]; - esql = new EmbeddedSQL (dbname, dbport, user, passwd); - - boolean keepon = true; - while(keepon) { - // These are sample SQL statements - System.out.println("WELCOME! Are you an existing user?"); - System.out.println("---------"); - System.out.println("0. Yes I am!"); - System.out.println("1. No, I need to register!"); - System.out.println("9. < EXIT (Stop the program)"); - - - switch (readChoice()){ - case 0: if(LogInQuery(esql) == 0 ) keepon = false; break; - case 1: if(RegisterQuery(esql) == 0 ) keepon = false; break; + public String executeStringQuery (String query) throws SQLException + { + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery (query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + System.out.println("--------------"); + while(rs.next()) + { + System.out.println(rs.getString(1)); + } + + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + String resultset = rs.getString(numCol); + System.out.println(resultset); + + stmt.close(); + return resultset; + } + + public void executeOrderQuery (String query) throws SQLException + { + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery(query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + + System.out.println("--------------"); + + while(rs.next()) + { + System.out.println("You are about to order:"); + System.out.println(rs.getString(1)); + } +/* + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + String resultset = rs.getString(numCol); + System.out.println(resultset); + + stmt.close(); + return resultset; +*/ + } + + + public int executeLoginQuery (String query) throws SQLException{ + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery (query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + rs.next(); + int resultset = Integer.parseInt(rs.getString(numCol)); + System.out.println(resultset); + + stmt.close(); + return resultset; + // iterates through the result set and output them to standard out. + /* boolean outputHeader = true; + while (rs.next()){ + if(outputHeader){ + for(int i = 1; i <= numCol; i++){ + System.out.print(rsmd.getColumnName(i) + "\t"); + } + System.out.println(); + outputHeader = false; + } + for (int i=1; i<=numCol; ++i) + System.out.print (rs.getString (i) + "\t"); + System.out.println (); + ++rowCount; + }//end while + stmt.close (); + return rowCount; + */ + } + + /** + * Method to execute an input query SQL instruction (i.e. SELECT). This + * method issues the query to the DBMS and outputs the results to + * standard out. + * + * @param query the input query string + * @return the number of rows returned + * @throws java.sql.SQLException when failed to execute the query + */ + public int executeQuery (String query) throws SQLException { + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery (query); + + /* + ** obtains the metadata object for the returned result set. The metadata + ** contains row and column info. + */ + ResultSetMetaData rsmd = rs.getMetaData (); + int numCol = rsmd.getColumnCount (); + int rowCount = 0; + + // iterates through the result set and output them to standard out. + boolean outputHeader = true; + while (rs.next()){ + if(outputHeader){ + for(int i = 1; i <= numCol; i++){ + System.out.print(rsmd.getColumnName(i) + "\t"); + } + System.out.println(); + outputHeader = false; + } + for (int i=1; i<=numCol; ++i) + System.out.print (rs.getString (i) + "\t"); + System.out.println (); + ++rowCount; + }//end while + stmt.close (); + return rowCount; + }//end executeQuery + + /** + * Method to close the physical connection if it is open. + */ + public void cleanup(){ + try{ + if (this._connection != null){ + this._connection.close (); + }//end if + }catch (SQLException e){ + // ignored. + }//end try + }//end cleanup + + /** + * The main execution method + * + * @param args the command line arguments this inclues the + */ + public static void main (String[] args) { + if (args.length != 4) { + System.err.println ( + "Usage: " + + "java [-classpath ] " + + EmbeddedSQL.class.getName () + + " "); + return; + }//end if + + Greeting(); + EmbeddedSQL esql = null; + try{ + // use postgres JDBC driver. + Class.forName ("org.postgresql.Driver").newInstance (); + // instantiate the EmbeddedSQL object and creates a physical + // connection. + String dbname = args[0]; + String dbport = args[1]; + String user = args[2]; + String passwd = args[3]; + esql = new EmbeddedSQL (dbname, dbport, user, passwd); + + boolean keepon = true; + while(keepon) { + // These are sample SQL statements + System.out.println("WELCOME! Are you an existing user?"); + System.out.println("---------"); + System.out.println("0. Yes I am!"); + System.out.println("1. No, I need to register!"); + System.out.println("9. < EXIT (Stop the program)"); + + + switch (readChoice()){ + case 0: if(LogInQuery(esql) == 0 ) keepon = false; break; + case 1: if(RegisterQuery(esql) == 0 ) keepon = false; break; case 9: return; - default : System.out.println("Unrecognized choice!"); break; - }//end switch + default : System.out.println("Unrecognized choice!"); break; + }//end switch }//end while //Will need to turn keepon to false after logging in @@ -301,11 +301,11 @@ public static void main (String[] args) { boolean homescreen = true; while(homescreen) { - System.out.println("Welcome to MovieNet"); - System.out.println("---------"); - System.out.println("0. See list of movies WORKING"); - System.out.println("1. Order a movie NOT WORKING"); - System.out.println("2. Rate a movie NOT WORKING"); + System.out.println("Welcome to MovieNet"); + System.out.println("---------"); + System.out.println("0. See list of movies WORKING"); + System.out.println("1. Order a movie NOT WORKING"); + System.out.println("2. Rate a movie NOT WORKING"); System.out.println("3. Look at wall NOT WORKING"); if( superUser ){ @@ -318,9 +318,9 @@ public static void main (String[] args) { /////////////////////////////////////////////////////////////CHANGES////////// if( !superUser ) System.out.println("8. Login as SuperUser"); /////////////////////////////////////////////////////////////CHANGES////////// - System.out.println("9. < EXIT (Stop the program)"); + System.out.println("9. < EXIT (Stop the program)"); - switch (readChoice()){ + switch (readChoice()){ case 0: ListMovies(esql); break; case 1: OrderMovie(esql); break; @@ -332,104 +332,104 @@ public static void main (String[] args) { /////////////////////////////////////////////////////////////CHANGES////////// case 9: return; - default : System.out.println("Unrecognized choice!"); break; + default : System.out.println("Unrecognized choice!"); break; } } - - }catch(Exception e) { - System.err.println (e.getMessage ()); - }finally{ - // make sure to cleanup the created table and close the connection. - try{ - if(esql != null) { - System.out.print("Disconnecting from database..."); - esql.cleanup (); - System.out.println("Done\n\nBye !"); - }//end if - }catch (Exception e) { - // ignored. - }//end try - }//end try - }//end main - - - - public static void ListMovies(EmbeddedSQL esql) - { - try{ - - String query = "SELECT title FROM Video"; - String output = esql.executeStringQuery(query); - - } - - catch(Exception e){ - System.err.println (e.getMessage ()); - } - } + + }catch(Exception e) { + System.err.println (e.getMessage ()); + }finally{ + // make sure to cleanup the created table and close the connection. + try{ + if(esql != null) { + System.out.print("Disconnecting from database..."); + esql.cleanup (); + System.out.println("Done\n\nBye !"); + }//end if + }catch (Exception e) { + // ignored. + }//end try + }//end try + }//end main + + + + public static void ListMovies(EmbeddedSQL esql) + { + try{ + + String query = "SELECT title FROM Video"; + String output = esql.executeStringQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } - public static void OrderMovie(EmbeddedSQL esql) - { - try{ - - Scanner user_input = new Scanner(System.in); - - System.out.print("Title of movie you would like to order: "); - - String query = "SELECT video_id, title FROM video WHERE title = "; - - - boolean flag = true; - while(flag) - { - String title = in.readLine(); - - if(title != null) - flag = false; - } - - - - esql.executeOrderQuery(query); - - } - - catch(Exception e){ - System.err.println (e.getMessage ()); - } - } - - - - // NOT FULLY IMPLEMENTED NEED TO GRAB VIDEO ID AND THEN RATE MOVIE - public static void RateMovie(EmbeddedSQL esql) - { - try{ - - System.out.print("Title of movie you would like to rate: "); - String title = in.readLine(); - System.out.print("What would you like to rate the movie (0 - 10): "); - String rating = in.readLine(); - - String query = "SELECT title FROM Video"; - - String output = esql.executeStringQuery(query); - - } - - catch(Exception e){ - System.err.println (e.getMessage ()); - } - } + public static void OrderMovie(EmbeddedSQL esql) + { + try{ + + Scanner user_input = new Scanner(System.in); + + System.out.print("Title of movie you would like to order: "); + + String query = "SELECT video_id, title FROM video WHERE title = "; + + + boolean flag = true; + while(flag) + { + String title = in.readLine(); + + if(title != null) + flag = false; + } + + + + esql.executeOrderQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } + + + + // NOT FULLY IMPLEMENTED NEED TO GRAB VIDEO ID AND THEN RATE MOVIE + public static void RateMovie(EmbeddedSQL esql) + { + try{ + + System.out.print("Title of movie you would like to rate: "); + String title = in.readLine(); + System.out.print("What would you like to rate the movie (0 - 10): "); + String rating = in.readLine(); + + String query = "SELECT title FROM Video"; + + String output = esql.executeStringQuery(query); + + } + + catch(Exception e){ + System.err.println (e.getMessage ()); + } + } - public static void Greeting(){ - System.out.println( - "\n\n*******************************************************\n" + - " User Interface \n" + - "*******************************************************\n"); + public static void Greeting(){ + System.out.println( + "\n\n*******************************************************\n" + + " User Interface \n" + + "*******************************************************\n"); }//end Greeting ///////////////////////////////////////////////////////////////START////////// @@ -587,29 +587,29 @@ else if( !isSeries ) /////////////////////////////////////////////////////////////CHANGES////////// /////////////////////////////////////////////////////////////CHANGES////////// //////////////////////////////////////////////////////////////END////////////// - /* - * Reads the users choice given from the keyboard - * @int - **/ - public static int readChoice() { - int input; - // returns only if a correct value is given. - do { - System.out.print("Please make your choice: "); - try { // read the integer, parse it and break. - input = Integer.parseInt(in.readLine()); - break; - }catch (Exception e) { - System.out.println("Your input is invalid!"); - continue; - }//end try - }while (true); - return input; - }//end readChoice - - public static int LogInQuery(EmbeddedSQL esql){ + /* + * Reads the users choice given from the keyboard + * @int + **/ + public static int readChoice() { + int input; + // returns only if a correct value is given. + do { + System.out.print("Please make your choice: "); + try { // read the integer, parse it and break. + input = Integer.parseInt(in.readLine()); + break; + }catch (Exception e) { + System.out.println("Your input is invalid!"); + continue; + }//end try + }while (true); + return input; + }//end readChoice + + public static int LogInQuery(EmbeddedSQL esql){ try{ - System.out.println("You are trying to Login as an existing user, enter your Username (Make sure it is only 9 chars long) or press 9 to go back"); + System.out.println("You are trying to Login as an existing user, enter your Username (Make sure it is only 9 chars long) or press 9 to go back"); //Keep asking the user to login if they want to log in while( true ){ System.out.println("Please specify your username or press 9 to go back:"); @@ -620,12 +620,12 @@ public static int LogInQuery(EmbeddedSQL esql){ String query = "SELECT COUNT(user_id) FROM users WHERE user_id='"; query += inputUser + "'"; - int output = esql.executeLoginQuery (query); - System.out.println("This is output" + output); + int output = esql.executeLoginQuery (query); + System.out.println("This is output" + output); //check if username is valid, if not 1 then either output = 0 or > 2 which is a problem if( output != 1 ) { - System.out.println("Sorry, that username isn't in our records. Please try again."); + System.out.println("Sorry, that username isn't in our records. Please try again."); continue; }//end username not found @@ -639,7 +639,7 @@ else if( output == 1 ){ //if password doesn't match records ask for username again if( output2 != 1 ){ - System.out.println("Sorry, the username and password don't match up. Please try again."); + System.out.println("Sorry, the username and password don't match up. Please try again."); continue; }//end password fail @@ -658,166 +658,166 @@ else if( output2 == 1 ){ return 0; }//end LoginQuery - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public static int RegisterQuery(EmbeddedSQL esql){ - System.out.println("You have selected to register yourself as a user. Please specify the following information."); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + public static int RegisterQuery(EmbeddedSQL esql){ + System.out.println("You have selected to register yourself as a user. Please specify the following information."); try{ - System.out.println("Enter a username you want (Username must be 9 chars or less long):"); - String input = in.readLine(); - - //if( input.length() > 9 ) { System.out.println("Invalid Username (more than 9 chars)"); continue; } - - String query = "INSERT INTO Users (user_id, password, first_name, middle_name, last_name, e_mail, street1, street2, state, country, zipcode, balance)" + - "VALUES ('" + input + "','"; - - System.out.println("Enter your password:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your first name:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your middle name:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your last name:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your email:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your street1:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your street2:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your state:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your country:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your zipcode:"); - input = in.readLine(); - query += input + "','"; - - System.out.println("Enter your balance:"); - input = in.readLine(); - query += input + "')"; - - esql.executeUpdate (query); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - return 1; - }//end catch - return 0; - }//end RegisterQuery - /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -/* - public static void RegisterQuery(EmbeddedSQL esql){ - try{ - String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " - + "WHERE C.sid=S.sid GROUP BY C.sid;"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query1 - - public static void Query2(EmbeddedSQL esql){ - try{ - String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " - + "WHERE C.sid=S.sid GROUP BY C.sid HAVING COUNT(C.pid)>=3;"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query2 - - public static void Query3(EmbeddedSQL esql){ - try{ - String query = "SELECT sname, COUNT(C.pid) FROM Suppliers S, Catalog C " - + "WHERE pid IN (SELECT pid FROM Parts WHERE color='Green') AND S.sid=C.sid " - + "GROUP BY sname"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query3 - - public static void Query4(EmbeddedSQL esql){ - try{ - String query = "SELECT P.pname, MAX(C.cost) FROM Parts P, Catalog C " - + "WHERE C.sid IN (SELECT temp.sid " - + "FROM (SELECT sid, pid FROM Catalog WHERE pid IN (SELECT pid FROM Parts WHERE color='Red')) as temp, " - + "(SELECT sid, pid FROM catalog WHERE PID IN (SELECT pid FROM Parts WHERE color='Green')) as temp2 " - + "WHERE temp.sid = temp2.sid) " - + "GROUP BY P.pname;"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query4 - - public static void Query5(EmbeddedSQL esql){ - try{ - String query = "SELECT DISTINCT pname FROM Parts " - + "WHERE pid IN (SELECT pid FROM Catalog WHERE cost<"; - - System.out.print("\tEnter cost: $"); - String input = in.readLine(); - query+=input; - query += ");"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query5 - - public static void Query6(EmbeddedSQL esql){ - try{ - String query = "SELECT address FROM Suppliers WHERE sid IN (" - + "SELECT sid FROM Catalog WHERE pid IN (" - + "SELECT pid FROM Parts WHERE pname='"; - - System.out.print("\tEnter name of part: "); - String input = in.readLine(); - query += input; - query += "'));"; - - int rowCount = esql.executeQuery (query); - System.out.println ("total row(s): " + rowCount); - - }catch(Exception e){ - System.err.println (e.getMessage ()); - } - }//end Query6 -*/ -}//end EmbeddedSQL + System.out.println("Enter a username you want (Username must be 9 chars or less long):"); + String input = in.readLine(); + + //if( input.length() > 9 ) { System.out.println("Invalid Username (more than 9 chars)"); continue; } + + String query = "INSERT INTO Users (user_id, password, first_name, middle_name, last_name, e_mail, street1, street2, state, country, zipcode, balance)" + + "VALUES ('" + input + "','"; + + System.out.println("Enter your password:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your first name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your middle name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your last name:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your email:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your street1:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your street2:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your state:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your country:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your zipcode:"); + input = in.readLine(); + query += input + "','"; + + System.out.println("Enter your balance:"); + input = in.readLine(); + query += input + "')"; + + esql.executeUpdate (query); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + return 1; + }//end catch + return 0; + }//end RegisterQuery + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/* + public static void RegisterQuery(EmbeddedSQL esql){ + try{ + String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " + + "WHERE C.sid=S.sid GROUP BY C.sid;"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query1 + + public static void Query2(EmbeddedSQL esql){ + try{ + String query = "SELECT C.sid, COUNT(C.pid) FROM Suppliers S, Catalog C " + + "WHERE C.sid=S.sid GROUP BY C.sid HAVING COUNT(C.pid)>=3;"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query2 + + public static void Query3(EmbeddedSQL esql){ + try{ + String query = "SELECT sname, COUNT(C.pid) FROM Suppliers S, Catalog C " + + "WHERE pid IN (SELECT pid FROM Parts WHERE color='Green') AND S.sid=C.sid " + + "GROUP BY sname"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query3 + + public static void Query4(EmbeddedSQL esql){ + try{ + String query = "SELECT P.pname, MAX(C.cost) FROM Parts P, Catalog C " + + "WHERE C.sid IN (SELECT temp.sid " + + "FROM (SELECT sid, pid FROM Catalog WHERE pid IN (SELECT pid FROM Parts WHERE color='Red')) as temp, " + + "(SELECT sid, pid FROM catalog WHERE PID IN (SELECT pid FROM Parts WHERE color='Green')) as temp2 " + + "WHERE temp.sid = temp2.sid) " + + "GROUP BY P.pname;"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query4 + + public static void Query5(EmbeddedSQL esql){ + try{ + String query = "SELECT DISTINCT pname FROM Parts " + + "WHERE pid IN (SELECT pid FROM Catalog WHERE cost<"; + + System.out.print("\tEnter cost: $"); + String input = in.readLine(); + query+=input; + query += ");"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query5 + + public static void Query6(EmbeddedSQL esql){ + try{ + String query = "SELECT address FROM Suppliers WHERE sid IN (" + + "SELECT sid FROM Catalog WHERE pid IN (" + + "SELECT pid FROM Parts WHERE pname='"; + + System.out.print("\tEnter name of part: "); + String input = in.readLine(); + query += input; + query += "'));"; + + int rowCount = esql.executeQuery (query); + System.out.println ("total row(s): " + rowCount); + + }catch(Exception e){ + System.err.println (e.getMessage ()); + } + }//end Query6 +*/ +}//end EmbeddedSQL From 5994136187031139dcbbecfd61782998c38005d1 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 14:56:21 -0700 Subject: [PATCH 09/13] Think I merge all of thomas work, but haven't error checked yet --- EmbeddedSQL.java | 249 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 170 insertions(+), 79 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index fcf6775..3331703 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -92,70 +92,105 @@ public void executeUpdate (String sql) throws SQLException { // close the instruction stmt.close (); - }//end executeUpdate - - public String executeStringQuery (String query) throws SQLException - { + }//end execute update + + int getVidId(String query) + { + try{ + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + + // issues the query instruction + ResultSet rs = stmt.executeQuery(query); + + while(rs.next()) + { + int vid_id = Integer.parseInt(rs.getString(1)); + return vid_id; + } + return -1; + }catch(Exception e){ + System.err.println (e.getMessage ());} + return -1; + }//end getVidID + + public int executeOrder (String query, int price) throws SQLException + { // creates a statement objectv Statement stmt = this._connection.createStatement (); // issues the query instruction - ResultSet rs = stmt.executeQuery (query); + ResultSet rs = stmt.executeQuery(query); - /* - ** obtains the metadata object for the returned result set. The metadata - ** contains row and column info. - */ - System.out.println("--------------"); while(rs.next()) { - System.out.println(rs.getString(1)); + int balance = Integer.parseInt(rs.getString(1)); + if(balance - price >= 0) + { + System.out.println("You would have negative balance if movie is ordered you cannot order movie"); + return -1; + } + else + { + String updatebal = "UPDATE users SET balance = balance - " + price + " WHERE user_id = '" + currentUser + "'"; + stmt.executeUpdate(updatebal); //I changed this part might be problematic + String newbalance = "SELECT balance FROM users WHERE user_id='" + currentUser + "'"; + System.out.print("New Balance if ordered: "); + executeStringQuery(newbalance, 1); + return 1; + } } - - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - rs.next(); - String resultset = rs.getString(numCol); - System.out.println(resultset); - - stmt.close(); - return resultset; + return -1; } - - public void executeOrderQuery (String query) throws SQLException + + public void executeStringQuery (String query, int line) throws SQLException { // creates a statement objectv Statement stmt = this._connection.createStatement (); // issues the query instruction - ResultSet rs = stmt.executeQuery(query); + ResultSet rs = stmt.executeQuery (query); /* ** obtains the metadata object for the returned result set. The metadata ** contains row and column info. */ - System.out.println("--------------"); - while(rs.next()) { - System.out.println("You are about to order:"); - System.out.println(rs.getString(1)); + System.out.println(rs.getString(line)); } -/* - ResultSetMetaData rsmd = rs.getMetaData (); - int numCol = rsmd.getColumnCount (); - rs.next(); - String resultset = rs.getString(numCol); - System.out.println(resultset); - - stmt.close(); - return resultset; -*/ } + + public int executeCheckTitle (String query) throws SQLException + { + try{ + // creates a statement objectv + Statement stmt = this._connection.createStatement (); + System.out.println("Connection Success"); + + // issues the query instruction + ResultSet rs = stmt.executeQuery(query); + System.out.println("Query exectued success"); + + System.out.println("--------------"); + + while(rs.next()) + { + System.out.print("Are you sure you would like to order (yes or no): "); + System.out.println(rs.getString(2) + " Price: " + rs.getString(3)); + String input = in.readLine(); + System.out.println(); + } + return Integer.parseInt(rs.getString(3)); + }catch(Exception e){ + System.err.println (e.getMessage ());} + return -1; + } - public int executeLoginQuery (String query) throws SQLException{ + public int executeLoginQuery (String query) throws SQLException + { // creates a statement objectv Statement stmt = this._connection.createStatement (); @@ -301,18 +336,23 @@ public static void main (String[] args) { boolean homescreen = true; while(homescreen) { + System.out.println("---------"); System.out.println("Welcome to MovieNet"); System.out.println("---------"); - System.out.println("0. See list of movies WORKING"); - System.out.println("1. Order a movie NOT WORKING"); + System.out.println("0. See list of movies"); + System.out.println("1. Order a movie"); System.out.println("2. Rate a movie NOT WORKING"); System.out.println("3. Look at wall NOT WORKING"); + System.out.println("4. Add to balance"); + System.out.println("5. List favorite movie"); + System.out.println("6. Add a favorite movie"); + //these will only be visible if you are super user if( superUser ){ System.out.println("\nSuper User Options:"); - System.out.println("5. Register a new movie"); - System.out.println("6. Delete an existing movie"); - System.out.println("7. Delete an existing user"); + System.out.println("10. Register a new movie WORKING"); + System.out.println("11. Delete an existing movie NOT WORKING"); + System.out.println("12. Delete an existing user NOT WORKING"); } /////////////////////////////////////////////////////////////CHANGES////////// @@ -323,15 +363,19 @@ public static void main (String[] args) { switch (readChoice()){ case 0: ListMovies(esql); break; case 1: OrderMovie(esql); break; - + case 2: break; + case 3: break; + case 4: IncreaseBalance(esql); break; + case 5: ListFavs(esql); break; + case 6: InsertFav(esql); break; /////////////////////////////////////////////////////////////CHANGES////////// - case 5: if( superUser ) {RegisterMovie(esql);}; break; - case 6: break; - case 7: break; case 8: if( !superUser ) {LoginAsSuper(esql);}; break; /////////////////////////////////////////////////////////////CHANGES////////// case 9: return; + case 10: if( superUser ) {RegisterMovie(esql);}; break; + case 11: break; + case 12: break; default : System.out.println("Unrecognized choice!"); break; } @@ -360,49 +404,98 @@ public static void main (String[] args) { public static void ListMovies(EmbeddedSQL esql) { try{ - String query = "SELECT title FROM Video"; - String output = esql.executeStringQuery(query); + String output = esql.executeStringQuery(query, 1); - } - - catch(Exception e){ + }catch(Exception e){ System.err.println (e.getMessage ()); } } - - public static void OrderMovie(EmbeddedSQL esql) + + public static void ListFavs(EmbeddedSQL esql) { try{ + String query = "SELECT title FROM video, likes WHERE likes.user_id = '" + currentUser + + "' AND likes.video_id = video.video_id"; + esql.executeStringQuery(query, 1); + }catch(Exception e){ + System.err.println (e.getMessage ());} + } + + public static void InsertFav(EmbeddedSQL esql) + { + try{ + System.out.print("Enter title of move you would like to add as favorite: "); + String title = in.readLine(); + String query = "SELECT video_id FROM video WHERE title = '" + title + "'"; + + int vid_id = esql.getVidId(query); + + System.out.println("GOT THE VID ID"); + query = "INSERT INTO likes (user_id, video_id) VALUES ('" + currentUser + "', " + vid_id + ")"; + esql.executeUpdate(query); + + System.out.println("New favorite added"); - Scanner user_input = new Scanner(System.in); - + }catch(Exception e){ + System.err.println (e.getMessage ());} + } + + public static void OrderMovie(EmbeddedSQL esql) + { + try{ System.out.print("Title of movie you would like to order: "); - - String query = "SELECT video_id, title FROM video WHERE title = "; + String title = in.readline(); + + //Grabbing title and will print it + String query = "SELECT * FROM video WHERE title = "; + query += title + "'"; + + int price = esql.executeCheckTitle(query); + + query = "SELECT balance FROM users WHERE user_id ='" + currentUser + "';"; + int temp = esql.executeOrder(query, price); + if( temp == 1 ) + System.out.println("Successfully ordered: " + title); + }catch(Exception e){ + System.err.println (e.getMessage ());} + } + + + public static void IncreaseBalance(EmbeddedSQL esql) + { + try{ - boolean flag = true; - while(flag) + System.out.print("How much would you like to add to balance?: "); + String stringbal = in.readLine(); + int intbal = Integer.parseInt(stringbal); + + if(intbal <= 0) { - String title = in.readLine(); - - if(title != null) - flag = false; + System.out.println("Must input number greater than $0"); + return; } - - - esql.executeOrderQuery(query); + System.out.println("About to update"); - } - - catch(Exception e){ - System.err.println (e.getMessage ()); - } - } + String query = "UPDATE users SET balance = balance +" + intbal + " WHERE user_id = '" + + currentUser + "'"; + + esql.executeUpdate(query); + System.out.println("UPDATED"); + + String newbalance = "SELECT balance FROM users WHERE user_id='" + currentUser + "'"; + System.out.print("New Balance after addition: "); + esql.executeStringQuery(newbalance, 1); + + return; + + }catch(Exception e){ + System.err.println (e.getMessage ());} + } // NOT FULLY IMPLEMENTED NEED TO GRAB VIDEO ID AND THEN RATE MOVIE public static void RateMovie(EmbeddedSQL esql) @@ -418,11 +511,8 @@ public static void RateMovie(EmbeddedSQL esql) String output = esql.executeStringQuery(query); - } - - catch(Exception e){ - System.err.println (e.getMessage ()); - } + }catch(Exception e){ + System.err.println (e.getMessage ());} } public static void Greeting(){ @@ -438,7 +528,8 @@ public static int LoginAsSuper(EmbeddedSQL esql) { try{ System.out.println("Verifying that you are a Super User"); - String query = "SELECT COUNT(super_user_id) FROM super_user WHERE super_user_id='" + currentUser + "';"; + String query = "SELECT COUNT(super_user_id) FROM super_user WHERE super_user_id='" + + currentUser + "';"; int resultset = esql.executeLoginQuery(query); From edc27225d31b701332fd1ab346a43309569e7bb3 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 15:01:30 -0700 Subject: [PATCH 10/13] Fixed code so it would compile, but there are still existing errors. --- EmbeddedSQL.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index 3331703..566c5d4 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -405,7 +405,7 @@ public static void ListMovies(EmbeddedSQL esql) { try{ String query = "SELECT title FROM Video"; - String output = esql.executeStringQuery(query, 1); + esql.executeStringQuery(query, 1); }catch(Exception e){ System.err.println (e.getMessage ()); @@ -445,7 +445,7 @@ public static void OrderMovie(EmbeddedSQL esql) { try{ System.out.print("Title of movie you would like to order: "); - String title = in.readline(); + String title = in.readLine(); //Grabbing title and will print it String query = "SELECT * FROM video WHERE title = "; @@ -509,7 +509,7 @@ public static void RateMovie(EmbeddedSQL esql) String query = "SELECT title FROM Video"; - String output = esql.executeStringQuery(query); + //String output = esql.executeStringQuery(query); //Cant use executeString This way!!!! }catch(Exception e){ System.err.println (e.getMessage ());} From d858311308c068e14a133150de6da4e9386eb0fc Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 17:15:59 -0700 Subject: [PATCH 11/13] implemented the superuser being able to delete users --- EmbeddedSQL.java | 52 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index 566c5d4..ab4a550 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -351,8 +351,7 @@ public static void main (String[] args) { if( superUser ){ System.out.println("\nSuper User Options:"); System.out.println("10. Register a new movie WORKING"); - System.out.println("11. Delete an existing movie NOT WORKING"); - System.out.println("12. Delete an existing user NOT WORKING"); + System.out.println("11. Delete an existing user NOT WORKING"); } /////////////////////////////////////////////////////////////CHANGES////////// @@ -374,8 +373,7 @@ public static void main (String[] args) { case 9: return; case 10: if( superUser ) {RegisterMovie(esql);}; break; - case 11: break; - case 12: break; + case 11: if( superUser ) {deleteUser(esql);}; break; default : System.out.println("Unrecognized choice!"); break; } @@ -674,6 +672,52 @@ else if( !isSeries ) return 0; } + //this will delete a user from the database (only if user is superUser) + public static int deleteUser(EmbeddedSQL esql) + { + try{ + System.out.println("In order to delete a specific movie instance you must specify the following information"); + while( true ) + { + System.out.println("Please specify a user_id that you want to delete or press 9 to cancel"); + String inputDelUser = in.readLine(); + + if( inputDelUser.equals("9") ) break; + else if( inputDelUser.equals(currentUser) ) + { + System.out.println("You cannot delete youself, sorry."); + continue; + } + + String query = "Select COUNT(user_id) FROM users WHERE user_id='" + inputDelUser + "';"; + + Statement stmt = esql._connection.createStatement (); + ResultSet rs = stmt.executeQuery(query); + ResultSetMetaData rsmd = rs.getMetaData(); + + //if the user does exist + if(rs.next() && Integer.parseInt(rs.getString(1)) == 1 ) + { + query = "DELETE FROM users WHERE user_id='" + inputDelUser + "';"; + esql.executeUpdate(query); + System.out.println("Successfully deleted user: " + inputDelUser ); + return 0; + }//end user exist + + //if user doesnt exist + else + { + System.out.println("Sorry that user doesn't exist. Please make sure you have the right user_id"); + continue; + }//end user doest exist + }//end while + + }catch(Exception e){ + System.err.println( e.getMessage() ); + return 1;} + return 0; + } + /////////////////////////////////////////////////////////////CHANGES////////// /////////////////////////////////////////////////////////////CHANGES////////// From 4ff8967be19953142bc9e0336f1b71d774e42539 Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 17:45:52 -0700 Subject: [PATCH 12/13] added genre implementation --- EmbeddedSQL.java | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index ab4a550..151a8ed 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -520,8 +520,6 @@ public static void Greeting(){ "*******************************************************\n"); }//end Greeting - ///////////////////////////////////////////////////////////////START////////// - /////////////////////////////////////////////////////////////CHANGES////////// public static int LoginAsSuper(EmbeddedSQL esql) { try{ @@ -549,6 +547,39 @@ else if( resultset == 1 ){ return 0; }//end LoginAsSuper + public static void queryGenre(EmbeddedSQL esql) + { + try{ + //Added some Genre code here////////////////////////////////////////////// + System.out.println("What is the genre of this video?"); + String inputGenre = in.readLine(); + + String getGenre = "SELECT genre_id FROM genre WHERE genre_name='" + inputGenre + "';"; + Statement stmt = esql._connection.createStatement(); + ResultSet rs = stmt.executeQuery(getGenre); + ResultSetMetaData rsmd = rs.getMetaData(); + + //only care if the genre doesn't already exist + if( !rs.next() ) + { + getGenre = "SELECT MAX(genre_id) FROM genre;"; + rs = stmt.executeQuery(getGenre); + rsmd = rs.getMetaData(); + + int genre_id; + if( rs.next() ) genre_id = Integer.parseInt(rs.getString(1))+1; + else genre_id = 1; + + esql.executeUpdate( "INSERT INTO genre (genre_id, genre_name) VALUES (" + genre_id + ", '" + inputGenre + "');" ); + rs.close(); + } + + //////////////////////////////////////////////////////////////////////// + + }catch(Exception e){ + System.err.println( e.getMessage() );} + } + //Register a new video, creating new video_id, season_id, and series_id if necessary public static int RegisterMovie(EmbeddedSQL esql) { @@ -558,10 +589,12 @@ public static int RegisterMovie(EmbeddedSQL esql) System.out.println("What is the title of this video?"); String inputTitle = in.readLine(); + + queryGenre(esql); System.out.println("Is this video part of a series (y/n)?"); String inputSeries = in.readLine(); - + String inputSeasonNum = ""; String inputEpNum = ""; String inputYear = ""; @@ -672,6 +705,8 @@ else if( !isSeries ) return 0; } + ///////////////////////////////////////////////////////////////START////////// + /////////////////////////////////////////////////////////////CHANGES////////// //this will delete a user from the database (only if user is superUser) public static int deleteUser(EmbeddedSQL esql) { @@ -701,6 +736,7 @@ else if( inputDelUser.equals(currentUser) ) query = "DELETE FROM users WHERE user_id='" + inputDelUser + "';"; esql.executeUpdate(query); System.out.println("Successfully deleted user: " + inputDelUser ); + rs.close(); return 0; }//end user exist @@ -708,6 +744,7 @@ else if( inputDelUser.equals(currentUser) ) else { System.out.println("Sorry that user doesn't exist. Please make sure you have the right user_id"); + rs.close(); continue; }//end user doest exist }//end while From b171293a17d1c125c2405bbb34dea91c6f5e149a Mon Sep 17 00:00:00 2001 From: jkaha001 Date: Tue, 11 Jun 2013 19:17:51 -0700 Subject: [PATCH 13/13] Fixed a bug for categorizing videos --- EmbeddedSQL.java | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/EmbeddedSQL.java b/EmbeddedSQL.java index 151a8ed..f23cf92 100644 --- a/EmbeddedSQL.java +++ b/EmbeddedSQL.java @@ -547,8 +547,10 @@ else if( resultset == 1 ){ return 0; }//end LoginAsSuper - public static void queryGenre(EmbeddedSQL esql) + ///SOME NEW FUNCTIONS HERE///////////////////////////////////////////////////////////////////////////////// + public static int queryGenre(EmbeddedSQL esql) { + int genre_id = -1; try{ //Added some Genre code here////////////////////////////////////////////// System.out.println("What is the genre of this video?"); @@ -559,14 +561,15 @@ public static void queryGenre(EmbeddedSQL esql) ResultSet rs = stmt.executeQuery(getGenre); ResultSetMetaData rsmd = rs.getMetaData(); + if( rs.next() ) genre_id = Integer.parseInt(rs.getString(1)); + //only care if the genre doesn't already exist - if( !rs.next() ) + else { getGenre = "SELECT MAX(genre_id) FROM genre;"; rs = stmt.executeQuery(getGenre); rsmd = rs.getMetaData(); - int genre_id; if( rs.next() ) genre_id = Integer.parseInt(rs.getString(1))+1; else genre_id = 1; @@ -576,10 +579,32 @@ public static void queryGenre(EmbeddedSQL esql) //////////////////////////////////////////////////////////////////////// + }catch(Exception e){ + System.err.println( e.getMessage() ); + return -1;} + return genre_id; + } + + public static void categorizeVideo(EmbeddedSQL esql, int video_id, int genre_id ) + { + System.out.println("VIDEOID: " + video_id + " GenreID: " + genre_id); + try{ + String getGenre = "SELECT COUNT(*) FROM categorize WHERE genre_id=" + genre_id + " AND video_id=" + video_id + ";"; + Statement stmt = esql._connection.createStatement(); + ResultSet rs = stmt.executeQuery(getGenre); + ResultSetMetaData rsmd = rs.getMetaData(); + + + //only care if this video_id isn't already in the list + if( !rs.next() || rs.getString(1).equals("0") ) + esql.executeUpdate( "INSERT INTO categorize (video_id, genre_id) VALUES (" + video_id + ", " + genre_id + ");" ); + }catch(Exception e){ System.err.println( e.getMessage() );} } + //////END OF SEGMENTED NEW FUNCTIONS/////////////////////////////////////////////////////////////////////// + //Register a new video, creating new video_id, season_id, and series_id if necessary public static int RegisterMovie(EmbeddedSQL esql) { @@ -590,8 +615,12 @@ public static int RegisterMovie(EmbeddedSQL esql) System.out.println("What is the title of this video?"); String inputTitle = in.readLine(); - queryGenre(esql); - + ///ADDEDD////////////////////////////////////////////////////////// + int genre_id = queryGenre(esql); + if( genre_id == -1 ) System.out.println("Problem when creating genre"); + ////ADDEDD////////////////////////////////////////////////////////// + + System.out.println("Is this video part of a series (y/n)?"); String inputSeries = in.readLine(); @@ -685,7 +714,6 @@ public static int RegisterMovie(EmbeddedSQL esql) esql.executeUpdate(insertVideo); - System.out.println("SEASON_ID " + series_id); stmt.close(); }//End if video is a series @@ -698,6 +726,9 @@ else if( !isSeries ) stmt.close(); }//End if video is not series + ///ADDDEDDD////////////////////////////////// + categorizeVideo(esql, video_id, genre_id); + ///ADDEDDDD///////////////////////////////// }catch(Exception e){ System.err.println( e.getMessage() ); return 1;