Advertisement
Jodyone

prob5

Apr 12th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1.  
  2.  
  3.     /*
  4.      * A Java program that uses the JDBC driver for SQLite
  5.      * to extract data from our movie database.
  6.      *
  7.      * Before compiling this program, you must download the JAR file for the
  8.      * SQLite JDBC Driver and add it to your classpath. See the Java-specific
  9.      * notes in the assignment for more details.
  10.      *
  11.      * In addition, the database file should be in the same folder
  12.      * as the program.
  13.      *
  14.      * Computer Science E-66
  15.      */
  16.  
  17.     import java.util.*;     // needed for the Scanner class
  18.     import java.sql.*;      // needed for the JDBC-related classes
  19.     import java.io.*;       // needed for the PrintStream class
  20.  
  21.     public class JavaSample {
  22.         public static void main(String[] args)
  23.           throws ClassNotFoundException, SQLException, FileNotFoundException
  24.         {
  25.             Scanner console = new Scanner(System.in);
  26.            
  27.             // Connect to the database.
  28.             System.out.print("name of database file: ");
  29.             String db_filename = console.next();
  30.         Class.forName("org.sqlite.JDBC");
  31.             Connection db = DriverManager.getConnection("jdbc:sqlite:" + db_filename);
  32.                    
  33.             // Create a PrintStream for the results file.
  34.             //System.out.print("name of results file: ");
  35.             String out_filename = "out.txt";
  36.             PrintStream outfile = new PrintStream(out_filename);
  37.            
  38.             // Create a Statement object and use it to execute the query.
  39.             Statement stmt = db.createStatement();
  40.            
  41.             String query = "SELECT *" +
  42.                            "FROM Movie ;";
  43.  
  44.             ResultSet results = stmt.executeQuery(query);
  45.            
  46.             int numMoviesReturned = 0;
  47.            
  48.             while (results.next()){
  49.                 String id = results.getString("id");
  50.                 String name = results.getString("name");
  51.                 String year = results.getString("year");
  52.                 String rating = results.getString("rating");
  53.                 String runtime = results.getString("runtime");
  54.                 String genre = results.getString("genre");
  55.                 String earnings_rank = results.getString("earnings_rank");
  56.                 String directors = "";
  57.                 String actors = "";
  58.                 String oscars = "";
  59.                 String oscYear = "";
  60.  
  61.                 String dirQuery = "SELECT director_id FROM Director WHERE Director.movie_id = " + id;
  62.                    
  63.                 Statement dirstmt = db.createStatement();
  64.                 ResultSet dirRes = dirstmt.executeQuery(dirQuery);
  65.              
  66.                 while(dirRes.next()){
  67.                    String dir_id = dirRes.getString(1);
  68.                    if(!directors.contains(dir_id))
  69.                        directors = directors + "P" + dir_id + " ";
  70.                 }
  71.                
  72.  
  73.                 String actorQuery = "SELECT actor_id FROM Actor WHERE Actor.movie_id = " + id;
  74.                
  75.                 Statement actorstmt = db.createStatement();
  76.                 ResultSet actorRes = actorstmt.executeQuery(actorQuery);
  77.                
  78.                 while(actorRes.next()){
  79.                    String act_id = actorRes.getString(1);
  80.                    if(!actors.contains(act_id))
  81.                       actors = actors + "P" + act_id + " ";
  82.                 }
  83.                
  84.                 String oscQuery = "SELECT type,year,person_id FROM oscar WHERE movie_id = " + id;
  85.                
  86.                 Statement oscstmt = db.createStatement();
  87.                 ResultSet oscarRes = oscstmt.executeQuery(oscQuery);  
  88.                
  89.                 while(oscarRes.next()){
  90.                    String type = oscarRes.getString("type");
  91.                    String year1 = oscarRes.getString("year");
  92.                    String oscarpid = oscarRes.getString("person_id");
  93.                    if (!type.equals(null)){
  94.                        
  95.                        
  96.                         if(type.matches("BEST-PICTURE"))
  97.                             type = "0000000";
  98.                         else {
  99.                             type = oscarpid;
  100.                         }
  101.                        type = "O" + year1 + type+ " ";
  102.                     }
  103.                    
  104.                    if(!oscars.contains(type))
  105.                       oscars = oscars + type;
  106.                 }
  107.                
  108.                 // write contents to xml file
  109.                 Object MovieTag = "  <movie ";
  110.                 outfile.print(MovieTag);
  111.                 outfile.println("id="+  "\"" + "M" + id + "\""+ " " + "directors="+  "\"" +  directors + "\"");
  112.                 outfile.print("         actors=");
  113.                 outfile.println("\"" + actors + "\"" + ">");
  114.                 if (oscars != null){
  115.                     outfile.print("         oscars=");
  116.                     outfile.println("\"" + oscars + "\"" + " ");
  117.                 }
  118.                 outfile.println("    <name>"+ name + "</name>");
  119.                 outfile.println("    <year>"+ year + "</year>");
  120.                 if(rating != null)
  121.                     outfile.println("    <rating>"+ rating + "</rating>");
  122.                 outfile.println("    <runtime>"+ runtime + "</runtime>");
  123.                 if(genre != null)
  124.                     outfile.println("    <genre>"+ genre +"</genre>");
  125.                 if(earnings_rank != null)
  126.                     outfile.println("    <earnings_rank>"+ earnings_rank+"</earnings_rank>");
  127.                 Object closeMovieTag = "  </movie>";
  128.                 outfile.println(closeMovieTag);            
  129.                 numMoviesReturned++;
  130.  
  131.             }
  132.      
  133.    
  134.             System.out.println("numMovies="+numMoviesReturned);
  135.             // Close the file and the database connection.
  136.             outfile.close();
  137.             db.close();
  138.         }
  139.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement