Advertisement
Guest User

Untitled

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