Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package pkgfilmassignment;
  2.  
  3. //import java.io.IOException;
  4. //import java.sql.DriverManager;
  5. //import java.sql.ResultSet;
  6. //import java.sql.Statement;
  7. //import java.sql.SQLException;
  8. //import java.util.Scanner;
  9.  
  10. import java.sql.*;
  11. import java.io.*;
  12. import java.util.*;
  13.  
  14. /**
  15.  *
  16.  * @author B00667769
  17.  */
  18. public class Film {
  19.  
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) throws SQLException, IOException {
  24.         // the following statement loads the Oracle  jdbc driver
  25.         // make sure it is in the CLASSPATH
  26.  
  27.         try {
  28.             Class.forName("oracle.jdbc.driver.OracleDriver");
  29.         } catch (ClassNotFoundException e) {
  30.             System.out.println("Could not load the driver");
  31.         }
  32.  
  33.         String user = "SYSTEM", pass = "Labuser1", host = "localhost", servicename;
  34.  
  35.         Scanner strInput = new Scanner(System.in);
  36.         System.out.println("Type userid, password, hostname or ipaddress: ");
  37.        
  38. //        do {
  39. //            System.out.println("Please enter the host url");
  40. //            try {
  41. //                host = KeyB.next();//allows input for user
  42. //                verdict = true;
  43. //            } catch (Exception ex) {
  44. //                System.out.println(ex.getMessage());
  45. //                verdict = false;
  46. //            }
  47. //        } while (verdict == false);//ensures correct format before allowing system to continue
  48. //
  49. //
  50. //
  51. //        verdict = false;
  52. //        do {
  53. //            System.out.println("Please enter your Username");
  54. //            try {
  55. //                user = KeyB.next();
  56. //                verdict = true;
  57. //            } catch (Exception ex) {
  58. //                System.out.println(ex.getMessage());
  59. //                verdict = false;
  60. //            }
  61. //
  62. //        } while (verdict == false);
  63. //
  64. //        verdict = false;
  65. //        do {
  66. //            System.out.println("Please enter your Password");
  67. //            try {
  68. //                pass = KeyB.next();
  69. //                verdict = true;
  70. //            } catch (Exception ex) {
  71. //                System.out.println(ex.getMessage());
  72. //                verdict = false;
  73. //            }
  74. //
  75. //        } while (verdict == false);
  76.         servicename = "orcl";
  77.         System.out.println(user + " " + pass + " " + host);
  78.        
  79.         //Connection conn = null;
  80.  
  81.         //  userid, password and hostname are obtained from the console
  82.         try ( //try-with-resources
  83.                 java.sql.Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":1521:" + servicename, user, pass))//end try
  84.         {//start try block
  85.  
  86.             Statement state = conn.createStatement();
  87.            
  88.            
  89.            
  90.             /* JDBC default is to commit each SQL statement as it is sent to the database.  
  91.             Setting autocommmit=false changes the default behaviour so that transactions
  92.             have to be committed explicity.
  93.              */
  94.             conn.setAutoCommit(false);
  95.  
  96.             // Creating a statement lets us issue commands against the connection.
  97.             Statement s = conn.createStatement();
  98.  
  99.             // Creating and populating Actor table
  100.             //s.executeUpdate("create table Actor( ActorName VARCHAR(20)  PRIMARY KEY,  Sex VARCHAR(6), Nationality VARCHAR(20), DOB DATE,  DebutFilmTitle VARCHAR(30))");
  101.             System.out.println("Created table Actor");
  102.  
  103.             //s.executeUpdate("insert into Actor values('George Clooney', 'Male', 'American', Date'1965-12-04','Casualty')");
  104.  
  105.             //conn.commit();
  106.             System.out.println("Inserted some actors");
  107.  
  108.             //ResultSet result = s.executeQuery("Select * From Actor");
  109. //            System.out.println("Results: ");
  110. //            while (result.next()) {
  111. //                System.out.println(result.getString(1) + "  " + result.getString(2) + "  " + result.getString(3) + "  " + result.getString(4) + "  " + result.getString(5));
  112. //            }
  113.  
  114.             // We end the transaction and the connection.
  115.             conn.commit();
  116.  
  117.             conn.close();
  118.         } catch (SQLException se) {
  119.  
  120.             se.printStackTrace(System.err);
  121.  
  122.         }//catch
  123.     }//end main
  124.    
  125.     public static String createTable(Statement state, Connection conn) throws SQLException, IOException{
  126.         conn.setAutoCommit(false);//transactions need to be carried out explicity
  127.  
  128.         state = conn.createStatement();
  129.  
  130.         state.executeUpdate("create table ASS2_FILM( Fnum  NOT NULL AUTO_INCREMENT VARCHAR(20)  PRIMARY KEY, Film SYS.XMLTYPE)");
  131.         conn.commit();
  132.  
  133.         return "Tables created";
  134.     }//end create table
  135.    
  136.      
  137.    
  138.  
  139. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement