Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JDBC LAB: Define a movie database application that allows a user to look up info

ID: 3686130 • Letter: J

Question

JDBC LAB:

Define a movie database application that allows a user to look up information about a movie and provide a review for a movie. Your application should provide the following features:

? List all movies that are rated a particular rating (PG, PG-13, R, etc) entered by the user.

? All a user to post a review for a particular movie. A review consists of review text and a star rating with o5 stars = Excellent o 4 stars = Good o 3 stars = Neutral o 2 stars = Poor o 1 star = Very Poor

? List all reviews for a movie and include the average star rating for the movie Your solution should read the database connection information (jdbc driver and connection url) from a properties file called database.properties.

You can create a console based application to implement this application or write a GUI interface to provide these movie database features described above. The only requirement is that the class containing the main method should be called MovieDBApp. Here is some information about the database table schema you will be using:

A set of commands to create and populate the MOVIE and MOVIE_REVIEW tables are provided below.

CONNECT 'jdbc:derby:C: empCSC251Lab11DB;create=true';

CREATE TABLE MOVIE (MOVIE_ID int not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),MOVIE_NAME varchar(100), RATED varchar(10));

CREATE TABLE MOVIE_REVIEW (REVIEW_ID int not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), MOVIE_ID int, REVIEW varchar(500), STARS int);

INSERT INTO MOVIE(MOVIE_NAME, RATED) values('Divergent', 'PG-13');

INSERT INTO MOVIE(MOVIE_NAME, RATED) values('Muppets Most Wanted', 'PG');

INSERT INTO MOVIE(MOVIE_NAME, RATED) values('The LEGO Movie', 'PG');

INSERT INTO MOVIE(MOVIE_NAME, RATED) values('Saving Mr. Banks', 'PG-13');

INSERT INTO MOVIE(MOVIE_NAME, RATED) values('Her', 'R');

Notice that in the CONNECT statement has a portion of the connection URL highlighted. This statement assumes that you will want to have the database files located in C: empCSC251Labb11DB. If you would like the database files to be located somewhere else on your system, you will need to update this statement. Also, when the MOVIE and MOVIE_REVIEW tables are created the MOVIE_ID and REVIEW_ID are set to be automatically generated by the database. As a result, any SQL statements to INSERT INTO the MOVIE or MOVIE_REVIEW table will not need to specify and ID. Instead, the database will automatically generate these values for you.

MOVIE MOVIE REVIEW REVIEN ID INT MOVIE ID MOVIE NAME VARCHAR (100) MOVIE_RATING VARCHAR (10) INI INT MOVIE_ID INT REVIEN STARS VARCHAR (500) INT

Explanation / Answer

  Properties props = new Properties();

   FileInputStream in =

      new FileInputStream("/external/configuration/dir/db.properties");

   props.load(in);

   in.close();

   String driver = props.getProperty("jdbc.driver");

   if (driver != null) {

       Class.forName(driver) ;

   }

   String url = props.getProperty("jdbc.url");

   String username = props.getProperty("jdbc.username");

   String password = props.getProperty("jdbc.password");

   Connection con = DriverManager.getConnection(url, username, password);

//Listing all movies that are rated a particular rating

String sql1 = " Select MOVIE_NAME from movie where MOVIE_RATING =’PG’";

myStmt1 = myConn.prepareStatement(sql1);

// Execute SQL query

myStmt1.executeUpdate();

String sql2 = " Select MOVIE_NAME from movie where MOVIE_RATING =’PG-13’";

myStmt2 = myConn.prepareStatement(sql2);

// Execute SQL query

myStmt2.executeUpdate();

String sql3 = = " Select MOVIE_NAME from movie where MOVIE_RATING =’R’";

myStmt3 = myConn.prepareStatement(sql3);

//Execute SQL query

myStmt3.executeUpdate();

// read user input from command line: REVIEW_ID,MOVIE_ID,REVIEW,STARS

Scanner scanner = new Scanner(System.in);

//taking query inputs from users

System.out.print("Enter review id: ");

int REVIEW_ID= scanner.nextInt();

System.out.print("Enter movie id ");

int MOVIE_ID = scanner.nextInt();

System.out.print("Enter review text ");

String REVIEW = scanner.nextLine();

System.out.print("Enter rating stars ");

int STARS = scanner.nextInt();

// Create a statement

String sql = "insert into MOVIE_REVIEW "

        + " (REVIEW_ID,MOVIE_ID,REVIEW,STARS)" + " values (?, ?, ?)";

myStmt = myConn.prepareStatement(sql);

// set param values

myStmt.setString(1, REVIEW_ID);

myStmt.setString(2, MOVIE_ID);

myStmt.setString(3, REVIEW);

myStmt.setString(4, STARS);

// Execute SQL query

myStmt.executeUpdate();

String sql4= " Select REVIEW_ID,MOVIE_ID,REVIEW,avg(STARS) from MOVIE_REVIEW where (select MOVIE_ID from MOVIE where MOVIE.MOVIE_ID = MOVIE_REVIEW.MOVIE_ID)";

myStmt4 = myConn.prepareStatement(sql4);

//Execute SQL query

myStmt4.executeUpdate();