Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 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.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38. public static void loadOracleJdbcDriver() {
  39. try {
  40. Class.forName("oracle.jdbc.driver.OracleDriver");
  41. } catch (ClassNotFoundException e) {
  42. System.out.println("Error -" + e.toString());
  43. System.out.println("Could not load the driver");
  44. }
  45.  
  46. }
  47.  
  48.  
  49. public static void connectToServer(String username, String password,
  50. String hostname, String serviceName) throws SQLException {
  51.  
  52. try {
  53. Scanner sc = new Scanner(System.in);
  54. System.out.println("Type userid, password, hostname, servicename: ");
  55. username = sc.next();
  56. password = sc.next();
  57. hostname = sc.next();
  58. serviceName = "orcl";
  59. System.out.println(username + " " + password + " " + hostname);
  60.  
  61. System.out.println("Connecting to Database...");
  62.  
  63. // userid, password and hostname are obtained from the console
  64. //try-with-resources
  65. try(
  66. Connection conn = DriverManager.getConnection("jdbc"
  67. + ":oracle:thin:"
  68. + username + "/" + password + "@" + hostname + ":1521/"
  69. + serviceName)){
  70.  
  71.  
  72. createFilmTable(conn);
  73. populateFilmTable(conn);
  74.  
  75. conn.setAutoCommit(false);
  76.  
  77. }
  78.  
  79. } catch (SQLException e) {
  80. System.out.println("Error + " + e.toString());
  81. System.out.println("Connection Failure");
  82. }
  83.  
  84.  
  85. }
  86.  
  87. public static void createFilmTable(Connection conn) throws SQLException {
  88.  
  89. Statement s = conn.createStatement();
  90.  
  91.  
  92. s.executeUpdate("DROP TABLE ASS2_FILM");
  93. System.out.println("Table Dropped");
  94. s.executeUpdate("CREATE TABLE ASS2_FILM(fnum VARCHAR(20) " +
  95. "CONSTRAINT fnum_PK PRIMARY KEY, " +
  96. " film SYS.XMLTYPE)");
  97.  
  98. System.out.println("Made");
  99.  
  100. }
  101.  
  102. public static void populateFilmTable(Connection conn)throws SQLException {
  103.  
  104. Statement s = conn.createStatement();
  105. String data_file = "src/DatabaseAssignment2/film.xml";
  106.  
  107.  
  108. try{
  109.  
  110. FileInputStream fis = new FileInputStream("src/DatabaseAssignment2/film.xml");
  111. XMLType xmlv = new XMLType(conn, fis);
  112. System.out.println(xmlv.getStringVal());
  113. String sqltxt = "INSERT INTO ASS2_FILM VALUES (?, ?)";
  114.  
  115. s.executeUpdate("INSERT INTO ASS2_FILM VALUES( '123', " +
  116. "sys.XMLType.createXML( ' " + xmlv.getStringVal() + " ' ))");
  117.  
  118. }
  119. catch(FileNotFoundException ex){
  120. System.out.println("Error + " + ex.toString());
  121. System.out.println("Connection Failure");
  122. }
  123.  
  124. }
  125.  
  126.  
  127. public static void showTitleOfAllFilms() {
  128.  
  129. }
  130.  
  131.  
  132. public static void showTitleOfAllFilmWithoutTitleTags() {
  133.  
  134. }
  135.  
  136.  
  137. public static void showNamesOfActorsWhoAppearedInGodfather() {
  138.  
  139. }
  140.  
  141.  
  142. public static void showTitleAndYearOfAllCrimeFilms() {
  143.  
  144. }
  145.  
  146.  
  147. public static void showTitleAndYearOfAllFilmsMarlonBrando() {
  148.  
  149. }
  150.  
  151. public static void showOneDirectorFilms() {
  152.  
  153. }
  154.  
  155.  
  156. public static void showTitleAndNamesMoreThanOneDirector() {
  157.  
  158. }
  159.  
  160.  
  161. public static void ShowTheNamesOfActorsAndSoleDirectorOfSameFilm() {
  162.  
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement