Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package homework4 ;
  2. import java.sql.* ;
  3. import java.util.ArrayList ;
  4. import java.util.List ;
  5.  
  6. public  class hw4
  7. {
  8.      //  Database credentials
  9.      static  final  String JDBC_DRIVER  =  "com.mysql.jdbc.Driver" ;
  10.      static  final  String DB_URL  =  "jdbc:mysql://localhost:3306/sakila" ;
  11.  
  12.      public  static  void main (String [] args)  throws  ClassNotFoundException
  13.      {
  14.          Connection conn  =  null ;
  15.          Statement stmt  =  null ;
  16.          try
  17.          {
  18.              //STEP 2: Register JDBC driver
  19.              Class. forName ("com.mysql.jdbc.Driver") ;
  20.    
  21.              //STEP 3: Open a connection
  22.             conn  =  DriverManager. getConnection (DB_URL, "root", "") ;
  23.            
  24.              //STEP 4: Execute a query
  25.             stmt  = conn. createStatement();
  26.            
  27.              //Query to get a list of all actor names and the number of movies they starred in
  28.              String sql = "SELECT actor.first_name, actor.last_name, actor.actor_id, count(*) as movie_count\r\n"  +
  29.                      "FROM film, film_actor, actor\r\n"  +
  30.                      "WHERE film.film_id = film_actor.film_id\r\n" +
  31.                      "AND film_actor.actor_id = actor.actor_id\r\n" +
  32.                      "AND film.film_id = film_actor.film_id\r\n" +
  33.                      "GROUP BY film_actor.actor_id";
  34.              ResultSet rs  = stmt. executeQuery (sql);
  35.            
  36.              int max_movie_count  =  0;
  37.  
  38.              while (rs. next ( ) )  //Find highest movie count
  39.              {
  40.                  int movie_count  = rs. getInt ( "movie_count" );
  41.                  if (movie_count > max_movie_count)
  42.                     max_movie_count = movie_count;
  43.              }
  44.             rs. first ( ) ;
  45.             List <String > max_actor_ids = new ArrayList <String >();
  46.             List <String > max_actor_names = new ArrayList<String >();
  47.            
  48.              while (rs. next())  //Find actor(s) with the highest movie count
  49.              {
  50.                  String actor_id  = rs. getString ("actor_id") ;
  51.                  String actor_name  = rs. getString ("first_name") + " " + rs. getString("last_name") ;
  52.                  int movie_count = Integer.parseInt (rs.getString("movie_count")) ;
  53.                  if (movie_count == max_movie_count)
  54.                  {
  55.                     max_actor_ids.add (actor_id) ;
  56.                     max_actor_names.add (actor_name);
  57.                  }
  58.              }
  59.            
  60.              for (int i = 0 ; i < max_actor_ids. size(); ++i) //Find top 3 most revenue earning movies for each
  61.              {                                                //actor that had starred in the most movies
  62.                  String actor_id  = max_actor_ids.get(i) ;
  63.                  String actor_name = max_actor_names.get(i) ;
  64.                  stmt = conn.createStatement();
  65.                  //Query to find the top 3 revenue earning movies for an actor
  66.                  sql  =  "SELECT film.title, rental_rate*count(*) as rental_value, rental_rate, count(*)\r\n" +
  67.                          "FROM film, inventory, rental\r\n" +
  68.                          "WHERE film.film_id IN (\r\n" +
  69.                          "   SELECT film.film_id\r\n" +
  70.                          "   FROM film, film_actor\r\n" +
  71.                          "   WHERE film.film_id = film_actor.film_id\r\n" +
  72.                          "    AND film_actor.actor_id =" + actor_id + "\r\n" + // <-- using the ID from the previous query
  73.                          "   )\r\n" +
  74.                          "AND film.film_id = inventory.film_id\r\n" +
  75.                          "AND inventory.inventory_id = rental.inventory_id\r\n" +
  76.                          "GROUP BY film.film_id\r\n" +
  77.                          "ORDER BY rental_value desc\r\n" +
  78.                          "LIMIT 3\r\n";
  79.                  rs = stmt.executeQuery(sql);
  80.                
  81.                  //Outputting actor information
  82.                  System.out.println("Actor who starred in the most movies: " + actor_name + "(actor ID "  + actor_id + ")");
  83.                  System.out.println("    acted in " + max_movie_count + " movies, top three revenue earning movies are:");
  84.                  int j = 0;
  85.                  while(rs.next()) //Print the top 3 revenue earning movies
  86.                  {
  87.                      ++j;
  88.                      String film_title  = rs.getString ( "title" );
  89.                      String rental_value  = rs.getString ("rental_value");
  90.                      System.out.println( "    Movie #"  + j  +  ": "  + film_title  +  ", $"  + rental_value );
  91.                  }
  92.              }
  93.             rs. close();
  94.             stmt.close();
  95.             conn.close();
  96.          }
  97.          catch (SQLException se2)
  98.          {
  99.             se2. printStackTrace ();
  100.          }
  101.      }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement