Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. package DatabaseAssignment2;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import java.sql.SQLException;
  10. import java.util.Scanner;
  11. import oracle.jdbc.*;
  12. import oracle.net.aso.s;
  13. import oracle.xdb.XMLType;
  14.  
  15.  
  16. public class DatabaseAssignment2 {
  17.  
  18. private static String username, password, hostname, serviceName;
  19. private static Connection conn;
  20. private static Scanner userInput;
  21.  
  22. public static void main(String[] args) throws SQLException {
  23. // TODO code application logic here
  24.  
  25. loadOracleJdbcDriver();
  26. connectToServer(username, password, hostname, serviceName);
  27. //Statement s = conn.createStatement();
  28.  
  29. }
  30.  
  31. public static void loadOracleJdbcDriver() {
  32. try {
  33. Class.forName("oracle.jdbc.driver.OracleDriver");
  34. } catch (ClassNotFoundException e) {
  35. System.out.println("Error -" + e.toString());
  36. System.out.println("Could not load the driver");
  37. }
  38.  
  39. }
  40.  
  41.  
  42. public static void connectToServer(String username, String password,
  43. String hostname, String serviceName) throws SQLException {
  44.  
  45. try {
  46. Scanner sc = new Scanner(System.in);
  47. System.out.println("Type userid, password, hostname, servicename: ");
  48. username = sc.next();
  49. password = sc.next();
  50. hostname = sc.next();
  51. serviceName = "orcl";
  52. System.out.println(username + " " + password + " " + hostname);
  53.  
  54. System.out.println("Connecting to Database...");
  55.  
  56. // userid, password and hostname are obtained from the console
  57. //try-with-resources
  58. try(
  59. Connection conn = DriverManager.getConnection("jdbc"
  60. + ":oracle:thin:"
  61. + username + "/" + password + "@" + hostname + ":1521/"
  62. + serviceName)){
  63.  
  64.  
  65. createFilmTable(conn);
  66. populateFilmTable(conn);
  67. showTitleOfAllFilms(conn);
  68. showTitleOfAllFilmWithoutTitleTags(conn);
  69. showNamesOfActorsWhoAppearedInGodfather(conn);
  70. showTitleAndYearOfAllCrimeFilms(conn);
  71. showTitleAndYearOfAllFilmsMarlonBrando(conn);
  72.  
  73. conn.setAutoCommit(false);
  74.  
  75. }
  76.  
  77. } catch (SQLException e) {
  78. System.out.println("Error + " + e.toString());
  79. System.out.println("Connection Failure");
  80. }
  81.  
  82.  
  83. }
  84.  
  85. public static void createFilmTable(Connection conn) throws SQLException {
  86.  
  87. Statement s = conn.createStatement();
  88.  
  89.  
  90. s.executeUpdate("DROP TABLE ASS2_FILM");
  91. System.out.println("Table Dropped");
  92. s.executeUpdate("CREATE TABLE ASS2_FILM(fnum VARCHAR(20) " +
  93. "CONSTRAINT fnum_PK PRIMARY KEY, " +
  94. " film SYS.XMLTYPE)");
  95.  
  96. System.out.println("Made");
  97.  
  98. }
  99.  
  100. public static void populateFilmTable(Connection conn)throws SQLException {
  101.  
  102. Statement s = conn.createStatement();
  103.  
  104.  
  105.  
  106. try{
  107.  
  108. FileInputStream fis = new FileInputStream("src/DatabaseAssignment2/film.xml");
  109. XMLType xmlv = new XMLType(conn, fis);
  110. System.out.println(xmlv.getStringVal());
  111. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  112.  
  113. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '01', " +
  114. "sys.XMLType.createXML( ' " + xmlv.getStringVal() + " ' ))");
  115.  
  116. FileInputStream fis2 = new FileInputStream("src/DatabaseAssignment2/film2.xml");
  117. XMLType xmlv2 = new XMLType(conn, fis2);
  118. System.out.println(xmlv2.getStringVal());
  119. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  120.  
  121. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '02', " +
  122. "sys.XMLType.createXML( ' " + xmlv2.getStringVal() + " ' ))");
  123.  
  124. FileInputStream fis3 = new FileInputStream("src/DatabaseAssignment2/film3.xml");
  125. XMLType xmlv3 = new XMLType(conn, fis3);
  126. System.out.println(xmlv.getStringVal());
  127. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  128.  
  129. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '03', " +
  130. "sys.XMLType.createXML( ' " + xmlv3.getStringVal() + " ' ))");
  131.  
  132. FileInputStream fis4 = new FileInputStream("src/DatabaseAssignment2/film4.xml");
  133. XMLType xmlv4 = new XMLType(conn, fis4);
  134. System.out.println(xmlv.getStringVal());
  135. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  136.  
  137. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '04', " +
  138. "sys.XMLType.createXML( ' " + xmlv4.getStringVal() + " ' ))");
  139.  
  140. System.out.println("Rows added to table----------------------------------------");
  141.  
  142.  
  143. }
  144. catch(FileNotFoundException ex){
  145. System.out.println("Error + " + ex.toString());
  146. System.out.println("Connection Failure");
  147. }
  148.  
  149. }
  150.  
  151.  
  152. public static void showTitleOfAllFilms(Connection conn) throws SQLException {
  153.  
  154. Statement s = conn.createStatement();
  155.  
  156. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('/film/title').getStringval() "
  157. + "FROM ASS2_FILM a " );
  158.  
  159. System.out.println("\n Fnum and /Film: \n");
  160.  
  161. while(result.next()){
  162. System.out.println(result.getString(1) + " " + result.getString(2) );
  163.  
  164. }
  165. }
  166.  
  167.  
  168.  
  169. public static void showTitleOfAllFilmWithoutTitleTags(Connection conn) throws SQLException {
  170.  
  171. Statement s = conn.createStatement();
  172.  
  173. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('film//title/text()').getStringval() "
  174. + "FROM ASS2_FILM a " );
  175.  
  176. System.out.println("\n Fnum and /Title: \n");
  177.  
  178. while(result.next()){
  179. System.out.println(result.getString(1) + " " + result.getString(2) );
  180.  
  181. }
  182.  
  183. }
  184.  
  185.  
  186. public static void showNamesOfActorsWhoAppearedInGodfather(Connection conn) throws SQLException {
  187.  
  188. Statement s = conn.createStatement();
  189.  
  190. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('film//actor/text()').getStringval() "
  191. + "FROM ASS2_FILM a "
  192. + "WHERE a.film.extract('film//title/text()').getStringval() = 'The Godfather'");
  193.  
  194. System.out.println("\n Fnum and /Actors: \n");
  195.  
  196. while(result.next()){
  197. System.out.println(result.getString(1) + " " + result.getString(2) );
  198.  
  199. }
  200.  
  201. }
  202.  
  203.  
  204. public static void showTitleAndYearOfAllCrimeFilms(Connection conn) throws SQLException {
  205.  
  206. Statement s = conn.createStatement();
  207.  
  208. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('/film/title/@year').getStringval() "
  209. + "FROM ASS2_FILM a "
  210. + "WHERE a.film.extract('/film//genres/genre[1]/text()').getStringval() = 'Crime'");
  211.  
  212. System.out.println("\n Fnum and /Film: \n");
  213.  
  214. while(result.next()){
  215. System.out.println(result.getString(1) + " " + result.getString(2));
  216.  
  217. }
  218. }
  219.  
  220.  
  221. public static void showTitleAndYearOfAllFilmsMarlonBrando(Connection conn) throws SQLException {
  222. Statement s = conn.createStatement();
  223.  
  224. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('/film/title/@year').getStringval() "
  225. + "FROM ASS2_FILM a "
  226. + "WHERE a.film.extract('/film//genres/genre[1]/text()').getStringval() = 'Crime'");
  227.  
  228. System.out.println("\n Fnum and /Film: \n");
  229.  
  230. while(result.next()){
  231. System.out.println(result.getString(1) + " " + result.getString(2));
  232.  
  233. }
  234. }
  235.  
  236.  
  237. public static void showOneDirectorFilms() {
  238.  
  239. }
  240.  
  241.  
  242. public static void showTitleAndNamesMoreThanOneDirector() {
  243.  
  244. }
  245.  
  246.  
  247. public static void ShowTheNamesOfActorsAndSoleDirectorOfSameFilm() {
  248.  
  249. }
  250.  
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement