Advertisement
Guest User

Untitled

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