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

Objective: Write a .java program that manages a Movie database. Requirements: Ea

ID: 3762343 • Letter: O

Question

Objective: Write a .java program that manages a Movie database.

Requirements:

Each movie needs to have the follow attributes:

Name

Year

Rating (1 to 5 stars)

Director Box Office

The Movie database must be able to:

Add a movie

Remove a movie by title

Sort movie by Title

Sort movie by Rating

Sort movie by Box Office Gross

Sort movies by a director (LastName)

Print to a database file (you may define your own protocol)

Read from a database file

Write a front end that will allow a user to use any of the features noted in the database

description.

Explanation / Answer

import java.sql.*;

public class MovieDB {

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
       Class.forName("com.mysql.jdbc.Driver");

       System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");
    
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      System.out.println("eneter ur choice");
      System.out.rpintln("1. retreive 2.Update 3. add 4. Delete. 5. Sort");
      Scanner sr= new Scanner(System.in);
       int choice=sr.nextInt();
       if (choice==1)
       {
           String sql = "Select * from Movies";
            ResultSet rs = stmt.executeQuery(sql);
       }else if(choice ==2){
           {
         String sql2 = "UPDATE Movies " +"SET rate=1 where name='avatar'";
         stmt.executeUpdate(sql);
          while(rs.next()){
         //Retrieve by column name
         int year = rs.getInt("year");
         int rate = rs.getInt("rate");
         String name = rs.getString("name");
         String director = rs.getString("director");

         //Display values
         System.out.print("year: " + year);
         System.out.print(", rate: " + rate);
         System.out.print(", name: " + name);
         System.out.println(", director: " + director);
      }
      rs.close();
       }else if(choice==3){
         String sql3 = "insert into movies vaues(' Avatar, 2013,1,Camaroon'"")");
         ResultSet rs1 = stmt.executeQuery(sql);
       }else if(choice==4){
         stmt.executeUpdate("delete from Movies where name = avatar");
       }else {
        String sql4="SELECT * FROM movies ORDER BY Name";
       }
        
  
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
      }// do nothing
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
   System.out.println("Goodbye!");
}
}