Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport java.io.* ;
- import java.sql.* ;
- import java.util.* ;
- class exercise_2_phossack {
- public static void main(String args[]){
- // Database connection stuff as per the examples
- InputStreamReader istream = new InputStreamReader(System.in) ;
- BufferedReader bufRead = new BufferedReader(istream) ;
- Connection conn = null;
- try
- {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://localhost/exercise_1_phossack";
- conn = DriverManager.getConnection(url,"phossack_apps", "220144177");
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- System.exit(1);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- System.exit(2);
- }
- /* - Movie Table. Here to show column list
- CREATE TABLE Movie (
- movie_id SERIAL PRIMARY KEY,
- movie_title char(100) NOT NULL,
- director_last_name char(50) NOT NULL,
- director_first_name char(50) NOT NULL,
- genre char(20) NOT NULL,
- CONSTRAINT check_genre CHECK(genre IN('Action', 'Adventure', 'Comedy', 'Romance', 'Science Fiction', 'Documentary', 'Drama', 'Horror')),
- media_type char(20),
- CONSTRAINT check_media_type CHECK (media_type IN('DVD', 'Blu-Ray')),
- release_date date,
- studio_name char(50),
- retail_price real CHECK (retail_price > 0),
- current_stock integer NOT NULL CHECK (current_stock > 0)
- );
- */
- String movieId ="";
- int movieId_int = 0;
- String movieTitle = "";
- String director_lname = "";
- String director_fname = "";
- String movieGenre = "";
- String mediaType = "";
- String releaseDate = "";
- String studioName = "";
- String retailPrice = "";
- double retailPrice_double = 0.0;
- String currentStock = "";
- int currentStock_int = 0;
- try {
- System.out.println("Please Enter the id for the new movie: ");
- String movieId_no = bufRead.readLine();
- int movieId_int = Integer.parseInt(movieId);
- System.out.println("Please Enter the title for the new movie: ");
- String movieTitle = bufRead.readLine();
- System.out.println("Please Enter the director's last name: ");
- String director_lname = bufRead.readLine();
- System.out.println("Please Enter the director's first name: ");
- String director_fname = bufRead.readLine();
- System.out.println("Please Enter the genre of the movie: ");
- String movieGenre = bufRead.readLine();
- System.out.println("Please Enter the media type(DVD or Blu-Ray: ");
- String mediaType = bufRead.readLine();
- System.out.println("Please Enter the movie's release date(YYYY-MM-DD): ");
- String releaseDate = bufRead.readLine();
- System.out.println("Please Enter the movie's studio: ");
- String studioName = bufRead.readLine();
- System.out.println("Please Enter the retail price of the movie: ");
- String retailPrice = bufRead.readLine();
- double retailPrice_double = Double.parseDouble(retailPrice);
- System.out.println("Please Enter the number of copies in stock: ");
- String currentStock = bufRead.readLine();
- int currentStock_int = Int.parseInt(currentStock);
- /*
- Add the additional data fields here
- */
- }catch (IOException err) {
- System.out.println(err);
- }catch(NumberFormatException err) {
- System.out.println(err);
- }
- //Now lets insert a new row of data
- System.out.println("\n****Inserting a New Movie*****");
- System.out.println();
- // First we specify our query
- Statement stmt = null;
- try {
- //Create a new statement object - notice the additional arguments for inserting
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- //Get all record in the employee table
- ResultSet uprs = stmt.executeQuery("SELECT * FROM movie");
- //Create a new row in the ResultSet object
- uprs.moveToInsertRow();
- //Add new employee's information to the new row of data
- /**********************************************************/
- // This is where you will need to update the code to include
- // the data entered by the user.
- uprs.updateInt("movie_id", movieId_int );
- uprs.updateString("movie_title",movieTitle );
- uprs.updateString("director_last_name", director_lname );
- uprs.updateString("director_first_name", director_fname );
- uprs.updateString("genre", movieGenre );
- uprs.updateString("media_type", mediaType );
- uprs.updateString("release_date", releaseDate );
- uprs.updateString("studio_name", studioName );
- uprs.updateDouble("retail_price", retailPrice_double );
- uprs.updateInt("current_stock", currentStock_int );
- /**********************************************************/
- //Insert the new row of data to the database
- uprs.insertRow();
- //Move the cursor back to the start of the ResultSet object
- uprs.beforeFirst();
- }catch (SQLException e ) {
- System.out.println(e);
- conn.close();
- System.exit(1);
- }
- System.out.println("\nQuery Executed Successfully...exiting");
- //Close the database connection
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement