Advertisement
Guest User

Untitled

a guest
May 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. mport java.io.* ;
  2. import java.sql.* ;
  3. import java.util.* ;
  4.  
  5.  
  6. class exercise_2_phossack {
  7.  
  8.  
  9.   public static void main(String args[]){
  10.  
  11.     // Database connection stuff as per the examples
  12.     InputStreamReader istream = new InputStreamReader(System.in) ;
  13.     BufferedReader bufRead = new BufferedReader(istream) ;
  14.     Connection conn = null;
  15.     try
  16.     {
  17.       Class.forName("org.postgresql.Driver");
  18.       String url = "jdbc:postgresql://localhost/exercise_1_phossack";
  19.       conn = DriverManager.getConnection(url,"phossack_apps", "220144177");
  20.     }
  21.     catch (ClassNotFoundException e)
  22.     {
  23.       e.printStackTrace();
  24.       System.exit(1);
  25.     }
  26.     catch (SQLException e)
  27.     {
  28.       e.printStackTrace();
  29.       System.exit(2);
  30.     }
  31.  
  32.     /* - Movie Table. Here to show column list
  33.         CREATE TABLE Movie (
  34.         movie_id SERIAL PRIMARY KEY,
  35.         movie_title char(100) NOT NULL,
  36.         director_last_name char(50) NOT NULL,
  37.         director_first_name char(50) NOT NULL,
  38.         genre char(20) NOT NULL,
  39.         CONSTRAINT check_genre CHECK(genre IN('Action', 'Adventure', 'Comedy', 'Romance', 'Science Fiction', 'Documentary', 'Drama', 'Horror')),
  40.         media_type char(20),
  41.         CONSTRAINT check_media_type CHECK (media_type IN('DVD', 'Blu-Ray')),
  42.         release_date date,
  43.         studio_name char(50),
  44.         retail_price real CHECK (retail_price > 0),
  45.     current_stock integer NOT NULL CHECK (current_stock > 0)
  46. );
  47.     */
  48.  
  49.     String movieId ="";
  50.     int movieId_int = 0;
  51.     String movieTitle = "";
  52.     String director_lname = "";
  53.     String director_fname = "";
  54.     String movieGenre = "";
  55.     String mediaType = "";
  56.     String releaseDate = "";
  57.     String studioName = "";
  58.     String retailPrice = "";
  59.     double retailPrice_double = 0.0;
  60.     String currentStock = "";
  61.     int currentStock_int = 0;    
  62.  
  63.  
  64.     try {
  65.       System.out.println("Please Enter the id for the new movie: ");
  66.       String movieId_no  = bufRead.readLine();
  67.       int movieId_int = Integer.parseInt(movieId);
  68.       System.out.println("Please Enter the title for the new movie: ");
  69.       String movieTitle = bufRead.readLine();
  70.  
  71.  
  72.       System.out.println("Please Enter the director's last name: ");
  73.       String director_lname  = bufRead.readLine();
  74.  
  75.       System.out.println("Please Enter the director's first name: ");
  76.       String director_fname = bufRead.readLine();
  77.  
  78.       System.out.println("Please Enter the genre of the movie: ");
  79.       String movieGenre = bufRead.readLine();
  80.  
  81.       System.out.println("Please Enter the media type(DVD or Blu-Ray: ");
  82.       String mediaType = bufRead.readLine();
  83.  
  84.       System.out.println("Please Enter the movie's release date(YYYY-MM-DD): ");
  85.       String releaseDate = bufRead.readLine();
  86.  
  87.       System.out.println("Please Enter the movie's studio: ");
  88.       String studioName = bufRead.readLine();
  89.  
  90.       System.out.println("Please Enter the retail price of the movie: ");
  91.       String retailPrice = bufRead.readLine();
  92.       double retailPrice_double = Double.parseDouble(retailPrice);
  93.       System.out.println("Please Enter the number of copies in stock: ");
  94.       String currentStock = bufRead.readLine();
  95.       int currentStock_int = Int.parseInt(currentStock);
  96.  
  97.  
  98.  
  99.  
  100.  
  101.       /*
  102.  
  103.       Add the additional data fields here
  104.  
  105.       */
  106.  
  107.  
  108.     }catch (IOException err) {
  109.       System.out.println(err);
  110.     }catch(NumberFormatException err) {
  111.       System.out.println(err);
  112.     }
  113.  
  114.  
  115.     //Now lets insert a new row of data
  116.  
  117.     System.out.println("\n****Inserting a New Movie*****");
  118.     System.out.println();
  119.  
  120.  
  121.     // First we specify our query
  122.     Statement stmt = null;
  123.     try {
  124.       //Create a new statement object - notice the additional arguments for inserting
  125.       stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  126.       //Get all record in the employee table
  127.       ResultSet uprs = stmt.executeQuery("SELECT * FROM movie");
  128.  
  129.       //Create a new row in the ResultSet object
  130.       uprs.moveToInsertRow();
  131.       //Add new employee's information to the new row of data
  132.  
  133.       /**********************************************************/
  134.       // This is where you will need to update the code to include
  135.       // the data entered by the user.
  136.    
  137.       uprs.updateInt("movie_id", movieId_int );
  138.       uprs.updateString("movie_title",movieTitle );
  139.       uprs.updateString("director_last_name", director_lname );
  140.       uprs.updateString("director_first_name", director_fname );
  141.       uprs.updateString("genre", movieGenre );
  142.       uprs.updateString("media_type", mediaType );
  143.       uprs.updateString("release_date", releaseDate );
  144.       uprs.updateString("studio_name", studioName );
  145.       uprs.updateDouble("retail_price", retailPrice_double );
  146.       uprs.updateInt("current_stock", currentStock_int );
  147.       /**********************************************************/
  148.       //Insert the new row of data to the database
  149.       uprs.insertRow();
  150.       //Move the cursor back to the start of the ResultSet object
  151.       uprs.beforeFirst();
  152.     }catch (SQLException e ) {
  153.       System.out.println(e);
  154.       conn.close();
  155.       System.exit(1);
  156.     }
  157.  
  158.     System.out.println("\nQuery Executed Successfully...exiting");
  159.     //Close the database connection
  160.   }
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement