Commit bcee6bc4 authored by Daniel Kim's avatar Daniel Kim
Browse files

temp commit

parent f1b7b034
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Scanner;
import java.sql.*;
......@@ -110,21 +111,56 @@ public class ClientApplication {
System.exit(0);
}
public static void printQueryResults(ResultSet rs) throws SQLException {
public static int printQueryResults(ResultSet rs, HashSet<String> attributes) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
for (int i = 1; i <= columnsNumber; i++) {
System.out.print(rsmd.getColumnName(i) + ", ");
int setItem = 0;
int colIter = 1;
HashSet<Integer> matchingColumns = new HashSet<Integer>();
for (int i = 1; i <= columnsNumber; i++) {
if (attributes.contains(rsmd.getColumnName(i))) {
matchingColumns.add(i);
System.out.print(rsmd.getColumnName(i) + ", ");
}
}
System.out.println();
while (rs.next()) {
setItem++;
System.out.print(setItem + ". ");
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
if (matchingColumns.contains(i)) {
if (colIter > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
colIter++;
}
}
colIter = 1;
System.out.println();
}
return setItem;
}
public static int printQueryResults(ResultSet rs, int row) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
int setItem = 0;
System.out.println();
while (rs.next()) {
setItem++;
if (setItem == row) {
for (int i = 1; i <= columnsNumber; i++) {
System.out.print(rsmd.getColumnName(i) + ": ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
System.out.println();
}
break;
}
}
System.out.println();
return setItem;
}
public static void search(String type, String keywords) {
......@@ -156,45 +192,181 @@ public class ClientApplication {
}
try {
// PreparedStatement query = null;
ResultSet rs = null;
Statement st = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String query = "";
String searchMovieQueryString = "select "
+ "title, "
+ "originalTitle, "
+ "group_concat(genre separator ', ') AS Genre, "
+ "year, "
+ "releaseDate, "
+ "duration, "
+ "description, "
+ "averageRating, "
+ "totalRating, "
+ "metascore, "
+ "incomeDomestic, "
+ "incomeWorldwide, "
+ "budget, "
+ "productionCompany, "
+ "language, "
+ "userID, "
+ "comment "
+ "from "
+ "Movies left outer join Income using (movieID) "
+ "left outer join Budget using (movieID) "
+ "left outer join ProductionCompaniesInMovies using (movieID) "
+ "left outer join LanguagesInMovies using (movieID) "
+ "left outer join GenresInMovies using (movieID) "
+ "left outer join Reviews using (movieID) where ";
String searchPersonQueryString = "select name, "
+ "birthName, "
+ "height, "
+ "bio, "
+ "birthDetails, "
+ "dateOfBirth, "
+ "dateOfDeath, "
+ "placeOfDeath, "
+ "reasonOfDeath "
+ "from People "
+ "left outer join DeadPeople using (personID) "
+ "left outer join PeopleInMovies using (personID) "
+ "left outer join Movies using (movieID) "
+ "where personID = '%s'";
HashSet<String> basicAttributes = new HashSet<String>();
switch (type) {
case "movie":
basicAttributes.add("title");
basicAttributes.add("genre");
basicAttributes.add("releaseDate");
basicAttributes.add("description");
basicAttributes.add("averageRating");
searchQueryString = searchMovieQueryString + "title like '%%%s%%' group by movieID %s limit 10";
keywords = keywords.replace("\'", "\\'");
query = String.format(searchQueryString, keywords, sortString);
rs = st.executeQuery(query);
int numResults = printQueryResults(rs, basicAttributes);
boolean exitLoop2 = false;
System.out.println();
System.out.println("Found " + numResults + " result(s). To show advanced details for a movie, type the corresponding item number.");
while (!exitLoop2) {
String command = input.nextLine();
if (Integer.parseInt(command) < 1 || Integer.parseInt(command) > numResults) {
System.out.println("Index out of range.");
}
else {
rs.beforeFirst();
printQueryResults(rs, Integer.parseInt(command));
exitLoop2 = true;
}
}
basicAttributes.clear();
break;
case "genre":
basicAttributes.add("title");
basicAttributes.add("genre");
basicAttributes.add("releaseDate");
basicAttributes.add("description");
basicAttributes.add("averageRating");
searchQueryString = "select "
+ "movieID, "
+ "title, "
+ "group_concat(genre separator ', ') AS genre, "
+ "releaseDate, "
+ "description, "
+ "averageRating "
+ "from Movies left outer join GenresInMovies using (movieID) where genre like '%%%s%%' group by movieID %s limit 10";
query = String.format(searchQueryString, keywords, sortString);
rs = st.executeQuery(query);
numResults = printQueryResults(rs, basicAttributes);
exitLoop2 = false;
System.out.println();
System.out.println("Found " + numResults + " result(s). To show advanced details for a movie, type the corresponding item number.");
while (!exitLoop2) {
String command = input.nextLine();
if (Integer.parseInt(command) < 1 || Integer.parseInt(command) > numResults) {
System.out.println("Index out of range.");
}
else {
rs.beforeFirst();
int setItem = 0;
while (rs.next()) {
setItem++;
if (setItem == Integer.parseInt(command)) {
String movieID = rs.getString(1);
searchQueryString = searchMovieQueryString + "movieID = '" + movieID + "'";
ResultSet rs2 = st.executeQuery(searchQueryString);
printQueryResults(rs2, 1);
break;
}
}
exitLoop2 = true;
}
}
} catch (Exception e) {
System.out.println(e);
}
basicAttributes.clear();
switch (type) {
case "movie":
searchQueryString = "select title, releaseDate, description, duration, averageRating, totalRating, metascore from "
+ "Movies left outer join Income using (movieID) "
+ "left outer join Budget using (movieID) "
+ "left outer join ProductionCompaniesInMovies using (movieID) "
+ "left outer join LanguagesInMovies using (movieID) "
+ "left outer join GenresInMovies using (movieID) "
+ "left outer join Reviews using (movieID) where title like '%%%s%%' %s limit 10";
break;
case "genre":
searchQueryString = "select title, releaseDate, genre, description from Movies inner join GenresInMovies using (movieID) where genre like '%%%s%%' %s limit 10";
break;
case "person":
searchQueryString = "select title, releaseDate, name, role, description from Movies inner join (People inner join PeopleInMovies using (personID)) using (movieID) where name like '%%%s%%' %s limit 10";
break;
default: break;
}
break;
case "person":
basicAttributes.add("name");
basicAttributes.add("birthName");
basicAttributes.add("height");
basicAttributes.add("birthDetails");
basicAttributes.add("dateOfBirth");
try {
Statement st = c.createStatement();
String query = String.format(searchQueryString, keywords, sortString);
ResultSet rs = st.executeQuery(query);
printQueryResults(rs);
searchQueryString = "select personID, name, birthName, height, bio, birthDetails, dateOfBirth, dateOfDeath, placeOfDeath, reasonOfDeath from People left outer join DeadPeople using (personID) left outer join PeopleInMovies using (personID) left outer join Movies using (movieID) where name like '%%%s%%' group by personID %s limit 10";
query = String.format(searchQueryString, keywords, sortString);
rs = st.executeQuery(query);
numResults = printQueryResults(rs, basicAttributes);
exitLoop2 = false;
System.out.println();
System.out.println("Found " + numResults + " result(s). To show advanced details for a person, type the corresponding item number.");
while (!exitLoop2) {
String command = input.nextLine();
if (Integer.parseInt(command) < 1 || Integer.parseInt(command) > numResults) {
System.out.println("Index out of range.");
}
else {
rs.beforeFirst();
int setItem = 0;
while (rs.next()) {
setItem++;
if (setItem == Integer.parseInt(command)) {
String personID = rs.getString(1);
// searchQueryString = searchPersonQueryString;
searchQueryString = String.format(searchPersonQueryString, personID);
System.out.println(searchQueryString);
ResultSet rs2 = st.executeQuery(searchQueryString);
printQueryResults(rs2, 1);
break;
}
}
exitLoop2 = true;
}
}
basicAttributes.clear();
break;
default: break;
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void advancedDetails(String movie) {
}
public static void addFavourite(String entity) {
String searchMovieQueryString = "select * from Movies where title like '%s%%'";
try {
......
......@@ -175,7 +175,7 @@ create procedure loadGenre() begin
declare i int default 0;
select count(*) from ImdbMovies into n;
set i = 0;
while i < 1000 do
while i < n do
set @s = concat(
"insert into GenresInMovies (movieID, genre) values ('",
(select movieID from ImdbMovies limit i, 1),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment