Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ass2_film;
  7.  
  8. import java.sql.*;
  9. import java.net.URL;
  10. import java.util.Scanner;
  11. import oracle.xml.sql.query.OracleXMLQuery;
  12. import oracle.xml.sql.dml.OracleXMLSave;
  13.  
  14.  
  15. /**
  16.  *
  17.  * @author Ryan
  18.  */
  19. public class ASS2_FILM {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.          // throws SQLException, IOException {
  26.  
  27.       // the following statement loads the Oracle jdbc driver
  28.  
  29.     try {
  30.       Class.forName ("oracle.jdbc.driver.OracleDriver");
  31.     } catch (ClassNotFoundException e) {
  32.         System.out.println ("Could not load the driver");
  33.       }
  34.    
  35.     String user, pass, host, servicename;
  36.    
  37.     Scanner sc = new Scanner(System.in);
  38.     System.out.println("Type userid, password, hostname or ipaddress: ");
  39.     user = sc.next();
  40.     pass = sc.next();
  41.     host = sc.next();
  42.     servicename = "xe";
  43.     System.out.println(user+" "+pass+" "+host);
  44.  
  45.     /*  userid, password and hostname are obtained from the console
  46.      *  It is assumed that the service name is XE and the database server
  47.      *  listens on the default port 1521.
  48.      */
  49.    
  50.     try (                                     //try-with-resources
  51.       Connection conn = DriverManager.getConnection
  52.         ("jdbc:oracle:thin:"+user+"/"+pass+"@"+host+":1521/"+servicename)
  53.     )
  54.     {
  55.        
  56.    
  57.     /* JDBC default is to commit each SQL statement as it is sent to the database.  Setting autocommmit=false changes the default
  58.        behaviour so that transactions have to be committed explicity.
  59.      */
  60.     conn.setAutoCommit(false);
  61.    
  62.     // Creating a statement lets us issue commands against the connection.
  63.  
  64.     Statement stmt = conn.createStatement();
  65.    
  66.     //stmt.executeUpdate("DROP TABLE SYSTEM.ASS2_FILM (FILM)");
  67.            
  68.         // Creating and populating ASS2_FILM table
  69.        
  70.         stmt.executeUpdate("create table ASS2_FILM (Fnum int NOT NULL AUTO INCREMENT, Film VARCHAR(255), constraint Fnum primary key (Fnum)))");
  71.         System.out.println("Created table ASS2_FILM");
  72.        
  73.         //stops working here i think lol
  74.        
  75.         stmt.executeUpdate("insert into SYSTEM.ASS2_FILM (FILM) values('The Godfather')");
  76.        
  77.    
  78.        
  79.        
  80.        
  81.        
  82.        
  83.          //RETRIEVALS
  84.  
  85.     //  Create an OracleXMLQuery instance by supplying an SQL query string
  86.     // (One can also supply a JDBC ResultSet object)
  87.  
  88.     OracleXMLQuery qry = new OracleXMLQuery(conn, ("select * from SYSTEM.ASS2_FILM (Film)"));  //right parenthesis was missing from SQL here - fixed now
  89.     qry.keepObjectOpen(true); // so that we can getXMLString more than once
  90.  
  91.     // Obtain the result as an XML string.  
  92.     // (Alternatively, obtain the result as a DOM tree.)
  93.     // One can also set several OracleXMLQuery options.
  94.  
  95.     String str = qry.getXMLString();
  96.     System.out.println(str);
  97.  
  98.  
  99.     //INSERTIONS
  100.    
  101. //might have to take this section out up until let's check l116
  102.  
  103.     // For inserting data, first create an OracleXMLSave object by passing table name
  104.     OracleXMLSave sav = new OracleXMLSave(conn, "SYSTEM.ASS2_FILM");
  105.  
  106.     // Set rowTag to ACTOR  because that is what we have done in the XML document
  107.     sav.setRowTag("FILM");
  108.  
  109.     // obtain the URL object for the data file
  110.     URL u = OracleXMLSave.getURL("src/ass2_film/DBDATAXML.txt");
  111.    
  112.    
  113.     String [] colnames = new String [1];
  114.     colnames[0] = "FILM";  
  115.    
  116.     sav.setUpdateColumnList(colnames);
  117.     //  insert data
  118.     sav.insertXML(u);
  119.  
  120.     /* This effectively executes
  121.  
  122.     *  INSERT INTO Actor VALUES('Amitabh Bachchan', 'Male', 'Indian', '10-Mar-1950', 'Hum Hindustano');
  123.     *  INSERT INTO Actor VALUES('Ian McKellen', 'Male', 'British', '25-Apr-1960', 'Julius Caesar');
  124.  
  125.     *  The default behaviour is to insert all data in the XML document.  
  126.     *  Missing attributes are set to NULL
  127.     */
  128.  
  129.     // Let's check
  130.     OracleXMLQuery qry2 = new OracleXMLQuery(conn, "select * from SYSTEM.ASS2_FILM");
  131.     System.out.println(qry2.getXMLString());
  132.    
  133.     /*  To insert data only into the specified columns from an XML document
  134.      *  (and ignore the remainder), we need to set a list of columns to be updated
  135.      */
  136.  
  137.    
  138.    
  139.     //sav.setUpdateColumnList(colnames);
  140.  
  141.     //u = OracleXMLSave.getURL("src/ass2_film/DBDATAXML.txt");
  142.  
  143.     //sav.insertXML(u);
  144.  
  145.     /* This effectively executes
  146.  
  147.     *  INSERT INTO Actor(ActorName, Nationality) VALUES('Shahrukh Khan', 'Indian');
  148.     *  INSERT INTO Actor(ActorName, Nationality) VALUES('Peter Sellers', 'British');
  149.     *  INSERT INTO Actor(ActorName, Nationality) VALUES('Emma Thompson', 'British');
  150.  
  151.     */
  152.  
  153.      //Let's check
  154.     OracleXMLQuery qry3 = new OracleXMLQuery(conn, "select * from Film");
  155.     System.out.println(qry3.getXMLString());
  156.  
  157.     sav.close();
  158.  
  159.    
  160.     //UPDATES
  161.  
  162.  
  163.     // Now change some rows according to the data in another XML file
  164.  
  165.     //  Having set update column list for sav, we are not sure how to 'unset' it.
  166.     //  Therefore create a new OracleXMLSave object.
  167.  
  168.     OracleXMLSave sav2 = new OracleXMLSave(conn, "Actor");
  169.     sav2.setRowTag("ACTOR");
  170.     u = OracleXMLSave.getURL("xmldata/min_filmdb_xml_update.txt");
  171.  
  172.     // Need to specify the  key columns for making updates
  173.     String[] keyColNames = new String [1];
  174.     keyColNames[0]="ACTORNAME";
  175.     sav2.setKeyColumnList(keyColNames);
  176.  
  177.     // Now update
  178.     sav2.updateXML(u);
  179.  
  180.     /* This effectively executes
  181.  
  182.     *  UPDATE Actor SET DOB='11-Sep-1946'. DebutFilmTitle='Saat Hindustani' WHERE ActorName = 'Amitabh Bachchan';
  183.     *  UPDATE Actor SET DebutFilmTitle='Anthony and Cleopatra' WHERE ActorName = 'Ian McKellen';
  184.  
  185.     */
  186.  
  187.     // Let's check
  188.     OracleXMLQuery qry4 = new OracleXMLQuery(conn, "select * from actor");
  189.     System.out.println(qry4.getXMLString());
  190.  
  191.     // To update only some of the columns (and ignore rest of the data in the XML document), we again need to specify
  192.     // an update column list.
  193.  
  194.     String [] updateColNames = new String [1];
  195.     updateColNames[0] = "DEBUTFILMTITLE";
  196.     sav2.setUpdateColumnList(updateColNames);
  197.     u = OracleXMLSave.getURL("xmldata/min_filmdb_xml_updsomecols.txt");
  198.  
  199.     sav2.updateXML(u);
  200.  
  201.     /* This effectively executes
  202.  
  203.     *  UPDATE Actor SET DebutFilmTitle='Amar, Akbar, Anthony' WHERE ActorName = 'Amitabh Bachchan';
  204.     *  UPDATE Actor SET DebutFilmTitle='King Lear' WHERE ActorName = 'Ian McKellen';
  205.  
  206.     */
  207.  
  208.  
  209.     // Let's check
  210.     OracleXMLQuery qry5 = new OracleXMLQuery(conn, "select * from actor");
  211.     System.out.println(qry5.getXMLString());
  212.  
  213.     sav2.close();
  214.  
  215.  
  216.     //DELETIONS
  217.  
  218.     OracleXMLSave sav3 = new OracleXMLSave(conn, "Actor");
  219.     sav3.setRowTag("ACTOR");
  220.  
  221.     URL u3 = OracleXMLSave.getURL("xmldata/min_filmdb_xml_delete.txt");
  222.  
  223.     sav3.deleteXML(u3);
  224.  
  225.     /* This effectively executes
  226.  
  227.     *  DELETE FROM  Actor WHERE ActorName = 'Amitabh Bachchan';
  228.     *  DELETE FROM  Actor WHERE ActorName = 'Judi Dench' AND DebutFilmTitle = 'Anthony and Cleopatra';
  229.  
  230.     *  Note that there is no row to match the 2nd deletetion criterion
  231.     */
  232.  
  233.     // Let's check
  234.     OracleXMLQuery qry6 = new OracleXMLQuery(conn, "select * from actor");
  235.     System.out.println(qry6.getXMLString());
  236.  
  237.     String[] keyColNames3 = new String [1];
  238.     keyColNames3[0]="ACTORNAME";
  239.     sav3.setKeyColumnList(keyColNames3);
  240.  
  241.     sav3.deleteXML(u3);
  242.  
  243.     /* Now
  244.  
  245.     *  DELETE FROM  Actor WHERE ActorName = 'Amitabh Bachchan';
  246.     *  DELETE FROM  Actor WHERE ActorName = 'Judi Dench';
  247.  
  248.     *  No match for the 1st deletion.  Dame Judi Dench now disappears
  249.     */
  250.  
  251.     // Let's check
  252.     OracleXMLQuery qry7 = new OracleXMLQuery(conn, "select * from actor");
  253.     System.out.println(qry7.getXMLString());
  254.  
  255.     conn.commit();
  256.  
  257.     qry.close();
  258.  
  259.     conn.close();
  260.    
  261.     }
  262.     catch (SQLException se) {
  263.        
  264.         se.printStackTrace(System.err);
  265.        
  266.     }
  267.   }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement