Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.sql.*;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- class exercise_2_jmurra40 {
- public static void main(String args[]) throws SQLException
- {
- String databaseName = "";
- String user = "";
- String userPassword = "";
- int movieId = 0;
- String movieTitle = "";
- String directorLastName = "";
- String directorFirstName = "";
- String genre = "";
- String mediaType = "";
- String tempReleaseDate;
- java.sql.Date releaseDate;
- String studioName = "";
- Double retailPrice = 0.0;
- int currentStock = 0;
- Scanner scan = new Scanner(System.in);
- System.out.println("\n******************************************************************");
- System.out.println(String.format("%40s", "Welcome to MovieDirect"));
- System.out.println("\n******************************************************************");
- System.out.print("database: ");
- databaseName = scan.nextLine();
- System.out.println(databaseName);
- databaseName = "jmurra40_Assignment_4"; //For testing purposes so i dont have to keep entering the db.
- System.out.print("\nuser: ");
- user = scan.nextLine();
- user = "jmurra40_apps"; //For testing purposes so i dont have to keep entering the user.
- System.out.print("\npassword: ");
- userPassword = scan.nextLine();
- userPassword = "220137719"; //For testing purposes so i dont have to keep entering the password.
- System.out.print("\nPlease enter the id for the new movie: ");
- movieId = scan.nextInt();
- System.out.print("\nPlease enter the title for the new movie: ");
- movieTitle = scan.nextLine();
- movieTitle = scan.nextLine();
- System.out.print("\nPlease enter the director's first name: ");
- directorFirstName = scan.nextLine();
- System.out.print("\nPlease enter the director's last name: ");
- directorLastName = scan.nextLine();
- System.out.print("\nPlease enter the genre of the movie: ");
- genre = scan.nextLine();
- System.out.print("\nPlease enter the media type: ");
- mediaType = scan.nextLine();
- System.out.print("\nPlease enter the movies's release date: ");
- tempReleaseDate = scan.nextLine();
- System.out.print("\nPlease enter the movies's studio: ");
- studioName = scan.nextLine();
- System.out.print("\nPlease enter the retail price of the Movie: ");
- retailPrice = scan.nextDouble();
- System.out.print("\nPlease enter the number of copies in stock: ");
- currentStock = scan.nextInt();
- releaseDate = java.sql.Date.valueOf(tempReleaseDate);
- Connection conn = null;
- try
- {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://localhost/" + databaseName;
- conn = DriverManager.getConnection(url, user, userPassword);
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- System.exit(1);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- System.exit(2);
- }
- 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();
- /*
- CREATE TABLE Movie(
- movie_id INT PRIMARY KEY,
- movie_title VARCHAR(100) NOT NULL,
- director_last_name VARCHAR(50) NOT NULL,
- director_first_name VARCHAR(50) NOT NULL,
- genre GENRE_TYPE NOT NULL ,
- media_type VARCHAR(20) NOT NULL CHECK (media_type = 'DVD' OR media_type = 'Blu-Ray'),
- release_date DATE,
- studio_name VARCHAR(50),
- retail_price REAL CHECK (retail_price > 0),
- current_stock INT CHECK (current_stock > 0)
- );
- */
- //Add new employee's information to the new row of data
- uprs.updateInt("movie_id", movieId);
- uprs.updateString("movie_title", movieTitle);
- uprs.updateString("director_last_name", directorLastName);
- uprs.updateString("director_first_name", directorFirstName);
- uprs.updateString("genre", genre);
- uprs.updateString("media_type", mediaType);
- uprs.updateDate("release_date", releaseDate);
- uprs.updateString("studio_name", studioName);
- uprs.updateDouble("retail_price", retailPrice);
- uprs.updateInt("current_stock", currentStock);
- //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("\nSuccess! A new entry for " + movieTitle + " has been entered into the database.");
- //Close the database connection
- conn.close();
- }
- }
Add Comment
Please, Sign In to add comment