Advertisement
Jodyone

Untitled

Apr 17th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.00 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 MovieToXML {
  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 = "moviesXml.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.             Object Xstring = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
  47.             outfile.println(Xstring);
  48.             outfile.println();
  49.            
  50.             Object moviesTag = "<movies>";
  51.             outfile.println(moviesTag);
  52.            
  53.             while (results.next()){
  54.                 String id = results.getString("id");
  55.                 String name = results.getString("name");
  56.                 String year = results.getString("year");
  57.                 String rating = results.getString("rating");
  58.                 String runtime = results.getString("runtime");
  59.                 String genre = results.getString("genre");
  60.                 String earnings_rank = results.getString("earnings_rank");
  61.                 String directors = "";
  62.                 String actors = "";
  63.                 String oscars = "";
  64.  
  65.                 String dirQuery = "SELECT director_id FROM Director WHERE Director.movie_id = " +"\"" + id + "\"" + ";";
  66.                    
  67.                 Statement dirstmt = db.createStatement();
  68.                 ResultSet dirRes = dirstmt.executeQuery(dirQuery);
  69.  
  70.                 while(dirRes.next()){
  71.                    String dir_id = dirRes.getString(1);
  72.                    if(!directors.contains(dir_id))
  73.                        directors = directors + "P" + dir_id + " ";
  74.                 }
  75.                
  76.                 String actorQuery = "SELECT actor_id FROM Actor WHERE movie_id = " + "\"" + id + "\"" + ";";
  77.                
  78.                 Statement actorstmt = db.createStatement();
  79.                 ResultSet actorRes = actorstmt.executeQuery(actorQuery);
  80.                
  81.                 while(actorRes.next()){
  82.                    String act_id = actorRes.getString(1);
  83.                    if(!actors.contains(act_id))
  84.                       actors = actors + "P" + act_id + " ";  
  85.                 }
  86.  
  87.                 String oscQuery = "SELECT type,year,person_id FROM oscar WHERE movie_id = " + "\"" + id + "\""  + ";";
  88.                
  89.                 Statement oscstmt = db.createStatement();
  90.                 ResultSet oscarRes = oscstmt.executeQuery(oscQuery);  
  91.                
  92.                 while(oscarRes.next()){
  93.                    String type = oscarRes.getString("type");
  94.                    String year1 = oscarRes.getString("year");
  95.                    String oscarpid = oscarRes.getString("person_id");
  96.                    if (!type.equals(null)){
  97.                          
  98.                         if(type.matches("BEST-PICTURE"))
  99.                             type = "0000000";
  100.                         else {
  101.                             type = oscarpid;
  102.                         }
  103.                        type = "O" + year1 + type+ " ";
  104.                     }
  105.                    
  106.                    if(!oscars.contains(type))
  107.                       oscars = oscars + type;
  108.                 }
  109.                
  110.                 // write contents to xml file
  111.                 Object MovieTag = "  <movie ";
  112.                 outfile.print(MovieTag);
  113.                
  114.                 outfile.println("id="+  "\"" + "M" + id + "\""+ " " + "directors="+  "\"" +  directors.substring(0, directors.length())+ "\"");
  115.                 outfile.print("         actors=");
  116.                 outfile.println("\"" + actors.substring(0, actors.length()) + "\"" + ">");
  117.                 if (oscars != ""){
  118.                     outfile.print("         oscars=");
  119.                     outfile.println("\"" + oscars.substring(0, oscars.length()) + "\"" + " ");
  120.                 }
  121.                 outfile.println("    <name>"+ name + "</name>");
  122.                 outfile.println("    <year>"+ year + "</year>");
  123.                 if(rating != null)
  124.                     outfile.println("    <rating>"+ rating + "</rating>");
  125.                 outfile.println("    <runtime>"+ runtime + "</runtime>");
  126.                 if(genre != null)
  127.                     outfile.println("    <genre>"+ genre +"</genre>");
  128.                 if(earnings_rank != null)
  129.                     outfile.println("    <earnings_rank>"+ earnings_rank+"</earnings_rank>");
  130.                 Object closeMovieTag = "  </movie>";
  131.                 outfile.println(closeMovieTag);            
  132.  
  133.             }
  134.            
  135.             Object closeMoviesTag = "</movies>";
  136.             outfile.println(closeMoviesTag);
  137.        
  138.             System.out.println("movies.xml has been written.");
  139.  
  140.             // Close the file and the database connection.
  141.             outfile.close();
  142.            
  143.              /*
  144.               *
  145.               *    people starts here
  146.               *    
  147.               */
  148.                
  149.                 String peopleXML = "peopleXml.txt";
  150.                 PrintStream outPplXML = new PrintStream(peopleXML);
  151.                
  152.                 // Create a Statement object and use it to execute the query.
  153.                 Statement pplStmt = db.createStatement();
  154.                
  155.                 String pplQuery = "SELECT *" +
  156.                                   "FROM Person ;";
  157.  
  158.                 ResultSet pplQResults = pplStmt.executeQuery(pplQuery);
  159.                  
  160.                 outPplXML.println(Xstring);
  161.                 outPplXML.println();
  162.                            
  163.                 Object peopleTag = "<people>";
  164.                 outPplXML.println(peopleTag);
  165.                 while (pplQResults.next()){
  166.                    
  167.                     String id = pplQResults.getString("id");
  168.                     String name = pplQResults.getString("name");
  169.                     String dob= pplQResults.getString("dob");
  170.                     String pob = pplQResults.getString("pob");
  171.                
  172.                     // write contents to xml file
  173.                     Object personTag = "  <person ";
  174.                     outPplXML.print(personTag);
  175.                     outPplXML.print("\"" +"id="+ "P" + id +"\"" );
  176.                    
  177.                     // Create a Statement object and use it to execute the Movies query.
  178.                     Statement dirStmt = db.createStatement();
  179.                    
  180.                     String directedQuery = "SELECT Movie.id FROM Director,Movie WHERE movie_id = Movie.id AND director_id = "+ "\"" + id + "\"" +";";
  181.  
  182.  
  183.                     ResultSet directedQResults = dirStmt.executeQuery(directedQuery);
  184.                    
  185.                     String directed = "";
  186.                     String dirMovieIds = "";
  187.                     while(directedQResults.next()){  
  188.                         dirMovieIds = directedQResults.getString(1);                       
  189.                        if(!directed.contains(dirMovieIds))
  190.                           directed =  directed + "M" + dirMovieIds + " ";
  191.                        
  192.                     }
  193.                     if(directed != ""){
  194.                         outPplXML.print(" directed=");
  195.                         outPplXML.println("\"" + directed + "\"" + ">");
  196.                     }
  197.                    
  198.                     // Create a Statement object and use it to execute the Movies query.
  199.                     Statement movStmt = db.createStatement();
  200.                    
  201.                     String actedInQuery = "SELECT Movie.id FROM Actor,Movie WHERE movie_id = Movie.id AND actor_id = "+ "\"" + id + "\"" +";";
  202.  
  203.  
  204.                     ResultSet actedInQResults = movStmt.executeQuery(actedInQuery);
  205.                    
  206.                     String actedIn = "";
  207.                    
  208.                    
  209.                     String movieIds= "";
  210.                     while(actedInQResults.next()){  
  211.                        movieIds = actedInQResults.getString(1);
  212.    
  213.                        if(!actedIn.contains(movieIds))
  214.                           actedIn =  actedIn + "M" + movieIds + " ";
  215.                        
  216.                     }
  217.                     if(actedIn != ""){
  218.                         if(directed != "")
  219.                             outPplXML.print("           actedIn=");
  220.                         else
  221.                             outPplXML.print(" actedIn=");
  222.                         outPplXML.println("\"" + actedIn + "\"" + ">");
  223.                     }
  224.                    
  225.                    
  226.                    
  227.                     // Create a Statement object and use it to execute the Movies query.
  228.                     Statement oscarsStmt = db.createStatement();
  229.                    
  230.                     String oscarsQuery = "SELECT Oscar.type,Oscar.year,Oscar.person_id FROM Oscar,Movie WHERE movie_id = id AND person_id = "+ "\"" + id + "\"" +";";
  231.                     String oscarsRecieved = "";
  232.                    
  233.  
  234.                     ResultSet oscarsQResults = oscarsStmt.executeQuery(oscarsQuery);
  235.                    
  236.                    
  237.                     while(oscarsQResults.next()) {
  238.                        
  239.                            String type = oscarsQResults.getString(1);
  240.                            String year = oscarsQResults.getString(2);
  241.                            String oscarpid = oscarsQResults.getString(3);
  242.                            if (!type.equals(null)){
  243.                                  
  244.                                 if(type.matches("BEST-PICTURE"))
  245.                                     type = "0000000";
  246.                                 else {
  247.                                     type = oscarpid;
  248.                                 }
  249.                                type = "O" + year + type+ " ";
  250.                             }
  251.                            
  252.                            if(!oscarsRecieved.contains(type))
  253.                               oscarsRecieved =  oscarsRecieved + type + " ";
  254.                     }
  255.                     if (oscarsRecieved != ""){
  256.                         outPplXML.print("           oscars=");
  257.                         outPplXML.println("\"" + oscarsRecieved + "\"" + " ");
  258.                     }  
  259.                     outPplXML.println("    <name>"+ name + "</name>");
  260.                     outPplXML.println("    <dob>"+ dob + "</dob>");
  261.                     outPplXML.println("    <pob>"+ pob + "</pob>");
  262.  
  263.                     Object closeoscarTag = "  </person>";
  264.                     outPplXML.println(closeoscarTag);  
  265.                 }
  266.                 Object closePeopleTag = "</people>";
  267.                 outPplXML.println(closePeopleTag);
  268.                 System.out.println("people.xml has been written.");
  269.                
  270.                 outPplXML.close();
  271.            
  272.          /*
  273.           *
  274.           *    oscar starts here
  275.           *    
  276.           */
  277.            
  278.             String oscarsXML = "oscarsXml.txt";
  279.             PrintStream outOscXML = new PrintStream(oscarsXML);
  280.            
  281.             // Create a Statement object and use it to execute the query.
  282.             Statement oscStmt = db.createStatement();
  283.            
  284.             String oscQuery = "SELECT *" +
  285.                            "FROM Oscar ;";
  286.  
  287.             ResultSet oscQResults = oscStmt.executeQuery(oscQuery);
  288.            
  289.            
  290.            
  291.             outOscXML.println(Xstring);
  292.             outOscXML.println();
  293.            
  294.            
  295.             Object oscarsTag = "<oscars>";
  296.             outOscXML.println(oscarsTag);
  297.             while (oscQResults.next()){
  298.                
  299.                 String movie_id = oscQResults.getString("movie_id");
  300.                 String person_id = oscQResults.getString("person_id");
  301.                 String type = oscQResults.getString("type");
  302.                 String year = oscQResults.getString("year");
  303.            
  304.                 // write contents to xml file
  305.                 Object oscarTag = "  <oscar ";
  306.                 outOscXML.print(oscarTag);
  307.                 String type1 = type;
  308.                 if (!type1.equals(null)){
  309.                        
  310.                    
  311.                     if(type1.matches("BEST-PICTURE"))
  312.                         type1 = "0000000";
  313.                     else {
  314.                         type1 = person_id;
  315.                     }
  316.                 type1 = "O" + year + type1;
  317.                 }
  318.                
  319.                 outOscXML.print("\"" +"id="+ type1 + "\"" );
  320.                 outOscXML.print(" movie_id=");
  321.                 outOscXML.print("\"" + "M" + movie_id + "\"" + " ");
  322.                 if(!type.matches("BEST-PICTURE"))
  323.                     outOscXML.print("person_id="+"\"" + "P" + person_id + "\"" + ">");
  324.                 outOscXML.println();
  325.                
  326.  
  327.                
  328.                 outOscXML.println("    <type>"+ type + "</type>");
  329.                 outOscXML.println("    <year>"+ year + "</year>");
  330.                
  331.  
  332.                 Object closeoscarTag = "  </oscar>";
  333.                 outOscXML.println(closeoscarTag);  
  334.             }
  335.             Object closeoscarsTag = "</oscars>";
  336.             outOscXML.println(closeoscarsTag);
  337.             System.out.println("oscars.xml has been written.");  
  338.             outOscXML.close();
  339.            
  340.             db.close();
  341.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement