Advertisement
Jodyone

prob 5

Apr 12th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1.  
  2.     /*
  3.      * A Java program that uses the JDBC driver for SQLite
  4.      * to extract data from our movie database.
  5.      *
  6.      * Before compiling this program, you must download the JAR file for the
  7.      * SQLite JDBC Driver and add it to your classpath. See the Java-specific
  8.      * notes in the assignment for more details.
  9.      *
  10.      * In addition, the database file should be in the same folder
  11.      * as the program.
  12.      *
  13.      * Computer Science E-66
  14.      */
  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 MovieToXML {
  22.    
  23.         public static void main(String[] args)
  24.           throws ClassNotFoundException, SQLException, FileNotFoundException
  25.         {
  26.             Scanner console = new Scanner(System.in);
  27.            
  28.             // Connect to the database.
  29.             System.out.print("name of database file: ");
  30.             String db_filename = console.next();
  31.             Class.forName("org.sqlite.JDBC");
  32.             Connection db = DriverManager.getConnection("jdbc:sqlite:" + db_filename);
  33.                    
  34.             // Create a PrintStream for the results file.
  35.            // System.out.print("name of results file: ");
  36.             String out_filename = "out.txt";
  37.             PrintStream outfile = new PrintStream(out_filename);
  38.             int numMoviesReturned = 0;
  39.            
  40.            
  41.             // Create a Statement object and use it to execute the query.
  42.             Statement stmt = db.createStatement();
  43.      
  44.             String query = "SELECT *" +
  45.                    "FROM Movie AS m LEFT OUTER JOIN Oscar AS o " +
  46.                    "ON m.id = o.movie_id;";
  47.    
  48.                            
  49.             ResultSet results = stmt.executeQuery(query);
  50.  
  51.             String nameCheck = "" ;
  52.  
  53.             Object Xstring = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
  54.             outfile.println(Xstring);
  55.             outfile.println();
  56.             movieContainer movieInfoBuffer = new movieContainer();
  57.             Object MoviesTag = "<movies>";
  58.             outfile.println(MoviesTag);
  59.             // Iterate over the tuples in the result and write them to the file.
  60.             while (results.next()) {
  61.                 String name = results.getString(2);    // 1 = leftmost column
  62.                 if(name.matches(nameCheck)){
  63.                     String dir = results.getString("director_id");
  64.                     if(!movieInfoBuffer.directors.contains(dir))
  65.                         movieInfoBuffer.directors = movieInfoBuffer.directors + " " + "P" + dir;
  66.                     String actor_id2 = results.getString("actor_id");
  67.                     if(!movieInfoBuffer.actors.contains(actor_id2))
  68.                         movieInfoBuffer.actors = movieInfoBuffer.actors + " " + "P" + actor_id2;
  69.                     String value = results.getString("type");
  70.                     String yearValue = results.getString(10);
  71.                     if (!value.equals(null)){
  72.                         value = results.getString(11);
  73.                         String opid = results.getString("person_id");
  74.                         if(value.matches("BEST-PICTURE"))
  75.                             value = "0000000";
  76.                         else {
  77.                             value = opid;
  78.                         }
  79.                         value = "O" + yearValue + value;
  80.                     }
  81.                     if (!movieInfoBuffer.oscars.contains(value))
  82.                         movieInfoBuffer.oscars = movieInfoBuffer.oscars + " " + value;
  83.                    
  84.                 }
  85.                 else{
  86.                         if (movieInfoBuffer.id != null){
  87.                             // write contents of mcontainer to xml file
  88.                             Object MovieTag = "  <movie ";
  89.                             outfile.print(MovieTag);
  90.                             outfile.println("id="+  "\"" + "M" + movieInfoBuffer.id + "\""+ " " + "directors="+
  91.                                             "\"" + "P" + movieInfoBuffer.directors + "\"");
  92.                             outfile.print("         actors=");
  93.                             outfile.println("\"" + movieInfoBuffer.actors + "\"" + ">");
  94.                             if (movieInfoBuffer.oscars != null){
  95.                                 outfile.print("         oscars=");
  96.                                 outfile.println("\"" + movieInfoBuffer.oscars + "\"" + " ");
  97.                             }
  98.                             outfile.println("    <name>"+ movieInfoBuffer.name + "</name>");
  99.                             outfile.println("    <year>"+ movieInfoBuffer.year + "</year>");
  100.                             if(movieInfoBuffer.rating != null)
  101.                                 outfile.println("    <rating>"+ movieInfoBuffer.rating + "</rating>");
  102.                             outfile.println("    <runtime>"+ movieInfoBuffer.runtime + "</runtime>");
  103.                             if(movieInfoBuffer.genre != null)
  104.                                 outfile.println("    <genre>"+ movieInfoBuffer.genre +"</genre>");
  105.                             if(movieInfoBuffer.earnings_rank != null)
  106.                                 outfile.println("    <earnings_rank>"+ movieInfoBuffer.earnings_rank+"</earnings_rank>");
  107.                             Object closeMovieTag = "  </movie>";
  108.                             outfile.println(closeMovieTag);
  109.                             movieInfoBuffer =  new movieContainer();
  110.                             numMoviesReturned++;
  111.                         }
  112.                         else{
  113.                             // add values to movieInfoBuffer fields
  114.                         //  populateBuffer(results,movieInfoBuffer);
  115.  
  116.                             //String name = results.getString("name");  
  117.                             String id = results.getString("id");
  118.                             String Query = "SELECT director_id FROM Director " +
  119.                                            "WHERE movie_id = " + id;
  120.                            
  121.                             Statement stmt2 = db.createStatement();
  122.                             ResultSet result2 = stmt2.executeQuery(Query);
  123.                             String director_id = result2.getString(1);
  124.                             String actor_id = results.getString("actor_id");
  125.                             String year = results.getString(2);
  126.                             String oscars = results.getString("type");
  127.  
  128.                             if (oscars !=null){
  129.                                 oscars = results.getString(11);
  130.                                 String opid = results.getString("person_id");
  131.                                 if(oscars.matches("BEST-PICTURE"))
  132.                                     oscars = "0000000";
  133.                                 else {
  134.                                     oscars = opid;
  135.                                 }
  136.                             }
  137.                                 // poulate Buffer
  138.                                 String rating = results.getString(4);
  139.                                 String runtime = results.getString(5);
  140.                                 String genre = results.getString("genre");
  141.                                 String earnings_rank = results.getString("earnings_rank");
  142.                                 String oscYear = results.getString(10);
  143.                                 movieInfoBuffer.id = id;
  144.                                 movieInfoBuffer.name = name;
  145.                                 movieInfoBuffer.year = year;
  146.                                 movieInfoBuffer.rating = rating;
  147.                                 movieInfoBuffer.runtime = runtime;
  148.                                 movieInfoBuffer.genre = genre;
  149.                                 movieInfoBuffer.earnings_rank = earnings_rank;
  150.                                 movieInfoBuffer.directors = director_id;
  151.                                 movieInfoBuffer.actors = "P" + actor_id;
  152.                                 if (oscars != null){
  153.                                     movieInfoBuffer.oscYear = oscYear;
  154.                                     movieInfoBuffer.oscars = "O"+ oscYear + oscars;
  155.                                 }
  156.                             nameCheck = name;
  157.                         }
  158.  
  159.                 }
  160.             }
  161.            
  162.             Object closeMoviesTag = "</movies>";
  163.             outfile.println(closeMoviesTag);
  164.             System.out.println("numMovies="+numMoviesReturned);
  165.             System.out.println("movies.xml has been written.");
  166.             // Close the file and the database connection.
  167.             outfile.close();
  168.             db.close();
  169.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement