Guest User

Untitled

a guest
May 18th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.sql.*;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat; 
  6.  
  7.  
  8. class exercise_2_jmurra40 {
  9.      public static void main(String args[]) throws SQLException
  10.      { 
  11.         String databaseName = "";
  12.         String user = "";
  13.         String userPassword = "";
  14.        
  15.         int movieId = 0;
  16.         String movieTitle = "";
  17.         String directorLastName = "";
  18.         String directorFirstName = "";
  19.         String genre = "";
  20.         String mediaType = "";
  21.         String tempReleaseDate;
  22.         java.sql.Date releaseDate;
  23.         String studioName = "";
  24.         Double retailPrice = 0.0;
  25.         int currentStock = 0;
  26.        
  27.         Scanner scan = new Scanner(System.in);
  28.        
  29.         System.out.println("\n******************************************************************");
  30.         System.out.println(String.format("%40s", "Welcome to MovieDirect"));
  31.         System.out.println("\n******************************************************************");
  32.        
  33.         System.out.print("database: ");
  34.         databaseName = scan.nextLine();
  35.         System.out.println(databaseName);
  36.         databaseName = "jmurra40_Assignment_4"; //For testing purposes so i dont have to keep entering the db.
  37.        
  38.         System.out.print("\nuser: ");
  39.         user = scan.nextLine();
  40.         user = "jmurra40_apps"; //For testing purposes so i dont have to keep entering the user.
  41.        
  42.         System.out.print("\npassword: ");
  43.         userPassword = scan.nextLine();
  44.         userPassword = "220137719"; //For testing purposes so i dont have to keep entering the password.
  45.        
  46.         System.out.print("\nPlease enter the id for the new movie: ");
  47.         movieId = scan.nextInt();
  48.         System.out.print("\nPlease enter the title for the new movie: ");
  49.         movieTitle = scan.nextLine();
  50.         movieTitle = scan.nextLine();
  51.         System.out.print("\nPlease enter the director's first name: ");
  52.         directorFirstName = scan.nextLine();
  53.         System.out.print("\nPlease enter the director's last name: ");
  54.         directorLastName = scan.nextLine();
  55.         System.out.print("\nPlease enter the genre of the movie: ");
  56.         genre = scan.nextLine();
  57.         System.out.print("\nPlease enter the media type: ");
  58.         mediaType = scan.nextLine();
  59.         System.out.print("\nPlease enter the movies's release date: ");
  60.         tempReleaseDate = scan.nextLine();
  61.         System.out.print("\nPlease enter the movies's studio: ");
  62.         studioName = scan.nextLine();
  63.         System.out.print("\nPlease enter the retail price of the Movie: ");
  64.         retailPrice = scan.nextDouble();
  65.         System.out.print("\nPlease enter the number of copies in stock: ");
  66.         currentStock = scan.nextInt();
  67.         releaseDate = java.sql.Date.valueOf(tempReleaseDate);
  68.        
  69.         Connection conn = null;
  70.         try
  71.         {
  72.  
  73.           Class.forName("org.postgresql.Driver");
  74.           String url = "jdbc:postgresql://localhost/" + databaseName;
  75.           conn = DriverManager.getConnection(url, user, userPassword);
  76.  
  77.         }
  78.         catch (ClassNotFoundException e)
  79.         {
  80.           e.printStackTrace();
  81.           System.exit(1);
  82.         }
  83.         catch (SQLException e)
  84.         {
  85.           e.printStackTrace();
  86.           System.exit(2);
  87.         }
  88.        
  89.         Statement stmt = null;
  90.         try {
  91.             //Create a new statement object - notice the additional arguments for inserting
  92.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  93.             //Get all record in the employee table
  94.             ResultSet uprs = stmt.executeQuery("SELECT * FROM Movie");
  95.             //Create a new row in the ResultSet object
  96.             uprs.moveToInsertRow();
  97.             /*
  98.             CREATE TABLE Movie(
  99.             movie_id    INT PRIMARY KEY,
  100.             movie_title VARCHAR(100) NOT NULL,
  101.             director_last_name VARCHAR(50) NOT NULL,
  102.             director_first_name VARCHAR(50) NOT NULL,
  103.             genre   GENRE_TYPE NOT NULL ,
  104.             media_type  VARCHAR(20) NOT NULL CHECK (media_type = 'DVD' OR media_type = 'Blu-Ray'),
  105.             release_date    DATE,
  106.             studio_name VARCHAR(50),
  107.             retail_price    REAL CHECK (retail_price > 0),
  108.             current_stock INT CHECK (current_stock > 0)    
  109.             );
  110.             */
  111.             //Add new employee's information to the new row of data
  112.             uprs.updateInt("movie_id", movieId);
  113.             uprs.updateString("movie_title", movieTitle);
  114.             uprs.updateString("director_last_name", directorLastName);
  115.             uprs.updateString("director_first_name", directorFirstName);
  116.             uprs.updateString("genre", genre);
  117.             uprs.updateString("media_type", mediaType);
  118.             uprs.updateDate("release_date", releaseDate);
  119.             uprs.updateString("studio_name", studioName);
  120.             uprs.updateDouble("retail_price", retailPrice);
  121.             uprs.updateInt("current_stock", currentStock);
  122.             //Insert the new row of data to the database
  123.             uprs.insertRow();
  124.             //Move the cursor back to the start of the ResultSet object
  125.             uprs.beforeFirst();
  126.         } catch (SQLException e ) {
  127.         System.out.println(e);
  128.         conn.close();
  129.         System.exit(1);
  130.         }
  131.        
  132.         System.out.println("\nSuccess! A new entry for " + movieTitle + " has been entered into the database.");
  133.         //Close the database connection
  134.         conn.close();
  135.    
  136.      }
  137.  
  138.  
  139. }
Add Comment
Please, Sign In to add comment