Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 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. public class DatabaseAssignment2 {
  16.  
  17. private static String username, password, hostname, serviceName;
  18. private static Connection conn;
  19. private static Scanner userInput;
  20.  
  21. public static void main(String[] args) throws SQLException {
  22. // TODO code application logic here
  23.  
  24. loadOracleJdbcDriver();
  25. connectToServer(username, password, hostname, serviceName);
  26. //Statement s = conn.createStatement();
  27.  
  28. }
  29.  
  30. public static void createMenu(Connection conn) throws SQLException {
  31.  
  32. try {
  33. conn = DriverManager.getConnection("jdbc"
  34. + ":oracle:thin:"
  35. + getUsername() + "/" + getPassword() + "@" + getHostname() + ":1521/"
  36. + getServicename());
  37. } catch (SQLException e) {
  38. System.out.println("Error-" + e.toString());
  39. }
  40. StringBuilder sb = new StringBuilder();
  41. int choice = 0;
  42. try {
  43.  
  44. sb.append("\nOracle Film Database"
  45. + "\n1 - Get the titles of all films"
  46. + "\n2 - Get the titles of all films without the surrounding <title> tag"
  47. + "\n3 - Get the names of all actors who appeard in the Godfather"
  48. + "\n4 - Get the title and year of all crime films"
  49. + "\n5 - Get the title and year of all films in which Marlon Brando has acted"
  50. + "\n6 - Get the titles of all films which have only one director"
  51. + "\n7 - Get the title and names of directors of all films which have more than one director"
  52. + "\n8 - Get the names of persons who have acted in a film and also been the sole director of the same film"
  53. + "\n9 - Exit Program");
  54.  
  55. do {
  56.  
  57. System.out.println(sb.toString());
  58.  
  59. System.out.println("Enter Option (1-9)");
  60. Scanner userInput = new Scanner(System.in);
  61. choice = userInput.nextInt();
  62.  
  63. switch (choice) {
  64. case 1:
  65.  
  66. showTitleOfAllFilms(conn);
  67.  
  68. break;
  69.  
  70. case 2:
  71. showTitleOfAllFilmWithoutTitleTags(conn);
  72.  
  73. break;
  74. case 3:
  75. showNamesOfActorsWhoAppearedInGodfather(conn);
  76.  
  77. break;
  78. case 4:
  79. showTitleAndYearOfAllCrimeFilms(conn);
  80.  
  81. break;
  82.  
  83. case 5:
  84. showTitleAndYearOfAllFilmsMarlonBrando(conn);
  85.  
  86. break;
  87. case 6:
  88. showOneDirectorFilms(conn);
  89.  
  90. break;
  91. case 7:
  92. showTitleAndNamesMoreThanOneDirector(conn);
  93.  
  94. break;
  95. case 8:
  96. ShowTheNamesOfActorsAndSoleDirectorOfSameFilm(conn);
  97.  
  98. break;
  99.  
  100. case 9:
  101. System.out.println("Exiting Program");
  102. System.exit(0);
  103.  
  104. break;
  105.  
  106. }
  107.  
  108. } while (choice != 9);
  109.  
  110. } catch (SQLException e) {
  111. System.out.println("Error - " + e.toString());
  112. }
  113.  
  114. }
  115.  
  116. public static void loadOracleJdbcDriver() {
  117. try {
  118. Class.forName("oracle.jdbc.driver.OracleDriver");
  119. } catch (ClassNotFoundException e) {
  120. System.out.println("Error -" + e.toString());
  121. System.out.println("Could not load the driver");
  122. }
  123.  
  124. }
  125.  
  126. public static void connectToServer(String username, String password,
  127. String hostname, String serviceName) throws SQLException {
  128.  
  129. try {
  130. Scanner sc = new Scanner(System.in);
  131. System.out.println("Type userid, password, hostname, servicename: ");
  132. username = sc.next();
  133. setUsername(username);
  134. password = sc.next();
  135. setPassword(password);
  136. hostname = sc.next();
  137. setHostname(hostname);
  138. serviceName = "orcl";
  139. setServiceName(serviceName);
  140. System.out.println(username + " " + password + " " + hostname);
  141.  
  142. System.out.println("Connecting to Database...");
  143.  
  144.  
  145. try (
  146. Connection conn = DriverManager.getConnection("jdbc"
  147. + ":oracle:thin:"
  148. + username + "/" + password + "@" + hostname + ":1521/"
  149. + serviceName)) {
  150.  
  151. createFilmTable(conn);
  152. populateFilmTable(conn);
  153. createMenu(conn);
  154.  
  155.  
  156. conn.setAutoCommit(false);
  157.  
  158. }
  159.  
  160. } catch (SQLException e) {
  161. System.out.println("Error - " + e.toString());
  162. System.out.println("Connection Failure");
  163. }
  164.  
  165. }
  166.  
  167. public static void createFilmTable(Connection conn) throws SQLException {
  168.  
  169. Statement s = conn.createStatement();
  170.  
  171. s.executeUpdate("DROP TABLE ASS2_FILM");
  172. System.out.println("Table Dropped");
  173. s.executeUpdate("CREATE TABLE ASS2_FILM(fnum VARCHAR(20) "
  174. + "CONSTRAINT fnum_PK PRIMARY KEY, "
  175. + " film SYS.XMLTYPE)");
  176.  
  177. System.out.println("Made");
  178.  
  179. }
  180.  
  181. public static void populateFilmTable(Connection conn) throws SQLException {
  182.  
  183. Statement s = conn.createStatement();
  184.  
  185. try {
  186.  
  187. FileInputStream fis = new FileInputStream("src/DatabaseAssignment2/film.xml");
  188. XMLType xmlv = new XMLType(conn, fis);
  189. System.out.println(xmlv.getStringVal());
  190. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  191.  
  192. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '01', "
  193. + "sys.XMLType.createXML( ' " + xmlv.getStringVal() + " ' ))");
  194.  
  195. FileInputStream fis2 = new FileInputStream("src/DatabaseAssignment2/film2.xml");
  196. XMLType xmlv2 = new XMLType(conn, fis2);
  197. System.out.println(xmlv2.getStringVal());
  198. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  199.  
  200. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '02', "
  201. + "sys.XMLType.createXML( ' " + xmlv2.getStringVal() + " ' ))");
  202.  
  203. FileInputStream fis3 = new FileInputStream("src/DatabaseAssignment2/film3.xml");
  204. XMLType xmlv3 = new XMLType(conn, fis3);
  205. System.out.println(xmlv.getStringVal());
  206. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  207.  
  208. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '03', "
  209. + "sys.XMLType.createXML( ' " + xmlv3.getStringVal() + " ' ))");
  210.  
  211. FileInputStream fis4 = new FileInputStream("src/DatabaseAssignment2/film4.xml");
  212. XMLType xmlv4 = new XMLType(conn, fis4);
  213. System.out.println(xmlv.getStringVal());
  214. //String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  215.  
  216. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '04', "
  217. + "sys.XMLType.createXML( ' " + xmlv4.getStringVal() + " ' ))");
  218.  
  219. System.out.println("Rows added to table----------------------------------------");
  220.  
  221. } catch (FileNotFoundException ex) {
  222. System.out.println("Error - " + ex.toString());
  223. System.out.println("Connection Failure");
  224. }
  225.  
  226. }
  227.  
  228. public static void showTitleOfAllFilms(Connection conn) throws SQLException {
  229.  
  230. Statement s = conn.createStatement();
  231.  
  232. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('/film/title').getStringval() "
  233. + "FROM ASS2_FILM a ");
  234.  
  235. System.out.println("\n Get the titles of all films;---------- \n");
  236.  
  237. while (result.next()) {
  238. System.out.println(result.getString(1) + " " + result.getString(2));
  239.  
  240. }
  241. }
  242.  
  243. public static void showTitleOfAllFilmWithoutTitleTags(Connection conn) throws SQLException {
  244.  
  245. Statement s = conn.createStatement();
  246.  
  247. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('film//title/text()').getStringval() "
  248. + "FROM ASS2_FILM a ");
  249.  
  250. System.out.println("\n Get the titles of all films without the surrounding <title> tag;---------- \n");
  251.  
  252. while (result.next()) {
  253. System.out.println(result.getString(1) + " " + result.getString(2));
  254.  
  255. }
  256.  
  257. }
  258.  
  259. public static void showNamesOfActorsWhoAppearedInGodfather(Connection conn) throws SQLException {
  260.  
  261. Statement s = conn.createStatement();
  262.  
  263. ResultSet result = s.executeQuery("SELECT a.fnum, a.film.extract('film//actor/text()').getStringval() "
  264. + "FROM ASS2_FILM a "
  265. + "WHERE a.film.extract('film//title/text()').getStringval() = 'The Godfather'");
  266.  
  267. System.out.println("\n Get the names of all actors who appeard in the Godfather;---------- \n");
  268.  
  269. while (result.next()) {
  270. System.out.println(result.getString(1) + " " + result.getString(2));
  271.  
  272. }
  273.  
  274. }
  275.  
  276. public static void showTitleAndYearOfAllCrimeFilms(Connection conn) throws SQLException {
  277.  
  278. Statement s = conn.createStatement();
  279.  
  280. ResultSet result = s.executeQuery("SELECT a.film.extract('/film/title/@year').getStringval, "
  281. + "a.film.extract('/film/title/text()').getStringval() "
  282. + "FROM ASS2_FILM a "
  283. + "WHERE a.film.extract('/film//genres/genre[1]/text()').getStringval() = 'Crime'");
  284.  
  285. System.out.println("\n Get the title and year of all crime films;---------- \n");
  286.  
  287. while (result.next()) {
  288. System.out.println(result.getString(1) + " " + result.getString(2));
  289.  
  290. }
  291. }
  292.  
  293. public static void showTitleAndYearOfAllFilmsMarlonBrando(Connection conn) throws SQLException {
  294. Statement s = conn.createStatement();
  295.  
  296. ResultSet result = s.executeQuery("SELECT a.film.extract('/film/title/@year').getStringval, "
  297. + "a.film.extract('/film/title/text()').getStringval() "
  298. + "FROM ASS2_FILM a "
  299. + "WHERE a.film.extract('/film//cast/performer[1]/actor/text()').getStringval() = 'Marlon Brando'");
  300.  
  301. System.out.println("\n Get the title and year of all films in which Marlon Brando has acted;---------- \n");
  302.  
  303. while (result.next()) {
  304. System.out.println(result.getString(1) + " " + result.getString(2));
  305.  
  306. }
  307. }
  308.  
  309. public static void showOneDirectorFilms(Connection conn) throws SQLException {
  310. Statement s = conn.createStatement();
  311.  
  312. ResultSet result = s.executeQuery("SELECT a.fnum,"
  313. + "a.film.extract('/film/title/text()').getStringval() FROM ASS2_FILM a "
  314. + "WHERE a.film.existsNode('/film//directors/director[2]') = 0");
  315.  
  316. System.out.println("\n Get the titles of all films which have only one director;---------- \n");
  317.  
  318. while (result.next()) {
  319. System.out.println(result.getString(1) + " " + result.getString(2));
  320.  
  321. }
  322. }
  323.  
  324. public static void showTitleAndNamesMoreThanOneDirector(Connection conn) throws SQLException {
  325. Statement s = conn.createStatement();
  326.  
  327. ResultSet result = s.executeQuery("SELECT a.film.extract('/film//director/text()').getStringval(),"
  328. + "a.film.extract('/film/title/text()').getStringval() FROM ASS2_FILM a "
  329. + "WHERE a.film.existsNode('/film//directors/director[2]') =1");
  330.  
  331. System.out.println("\n Get the title and names of directors of all films which have more than one director;---------- \n");
  332.  
  333. while (result.next()) {
  334. System.out.println(result.getString(1) + " " + result.getString(2));
  335.  
  336. }
  337. }
  338. public static void ShowTheNamesOfActorsAndSoleDirectorOfSameFilm(Connection conn) throws SQLException {
  339. Statement s = conn.createStatement();
  340.  
  341. ResultSet result = s.executeQuery("SELECT a.film.extract('/film//director/text()').getStringval() FROM ASS2_FILM a "
  342. + "WHERE a.film.existsNode('/film//directors/director[2]') =0"
  343. + "AND a.film.extract('/film//directors/director[1]').getStringval() = a.film.extract('/film//cast/performer/actor').getStringval()");
  344.  
  345. System.out.println("Get the names of persons who have acted in a film and also been the sole director of the same film;---------- \n");
  346.  
  347. while (result.next()) {
  348. System.out.println(result.getString(1) + " " + result.getString(2));
  349.  
  350. }
  351. }
  352. public static String getUsername() {
  353. return username;
  354. }
  355.  
  356. public static void setUsername(String username) {
  357. DatabaseAssignment2.username = username;
  358. }
  359.  
  360. public static String getPassword() {
  361. return password;
  362. }
  363.  
  364. public static void setPassword(String password) {
  365. DatabaseAssignment2.password = password;
  366. }
  367.  
  368. public static String getHostname() {
  369. return hostname;
  370. }
  371.  
  372. public static void setHostname(String hostname) {
  373. DatabaseAssignment2.hostname = hostname;
  374. }
  375.  
  376. public static String getServicename() {
  377. return serviceName;
  378. }
  379.  
  380. public static void setServiceName(String serviceName) {
  381. DatabaseAssignment2.serviceName = serviceName;
  382. }
  383.  
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement