Welcome to the Movie Database program. This program will help you create a simple database to store and manage information about movies such as title, genre, release year, and director.
Introduction
This program is designed to help store and manage information about movies. It allows the user to add new movies, view details of movies in the database, update information, and delete movies from the database. The goal is to understand how to use basic database operations such as create, read, update, and delete (CRUD) in Java.
Objective
The main objectives of this project are:
- Learn how to create a movie database in Java.
- Perform CRUD operations using Java.
- Get hands-on experience with basic Java programming concepts.
Code
import java.sql.*;
import java.util.Scanner;
public class MovieDatabase {
private static Connection connect() {
try {
String url = "jdbc:sqlite:movies.db";
Connection conn = DriverManager.getConnection(url);
return conn;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
private static void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS movies (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"title TEXT NOT NULL," +
"genre TEXT NOT NULL," +
"release_year INTEGER NOT NULL," +
"director TEXT NOT NULL);";
try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
stmt.execute(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void insertMovie(String title, String genre, int year, String director) {
String sql = "INSERT INTO movies(title, genre, release_year, director) VALUES(?, ?, ?, ?)";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, title);
pstmt.setString(2, genre);
pstmt.setInt(3, year);
pstmt.setString(4, director);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void listMovies() {
String sql = "SELECT * FROM movies";
try (Connection conn = connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Title: " + rs.getString("title"));
System.out.println("Genre: " + rs.getString("genre"));
System.out.println("Year: " + rs.getInt("release_year"));
System.out.println("Director: " + rs.getString("director"));
System.out.println("---------------------------");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void updateMovie(int id, String title, String genre, int year, String director) {
String sql = "UPDATE movies SET title = ?, genre = ?, release_year = ?, director = ? WHERE id = ?";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, title);
pstmt.setString(2, genre);
pstmt.setInt(3, year);
pstmt.setString(4, director);
pstmt.setInt(5, id);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void deleteMovie(int id) {
String sql = "DELETE FROM movies WHERE id = ?";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
createTable();
while (true) {
System.out.println("1. Add Movie");
System.out.println("2. List Movies");
System.out.println("3. Update Movie");
System.out.println("4. Delete Movie");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter movie title: ");
String title = scanner.nextLine();
System.out.print("Enter movie genre: ");
String genre = scanner.nextLine();
System.out.print("Enter release year: ");
int year = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter director name: ");
String director = scanner.nextLine();
insertMovie(title, genre, year, director);
break;
case 2:
listMovies();
break;
case 3:
System.out.print("Enter movie ID to update: ");
int updateId = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new movie title: ");
String updateTitle = scanner.nextLine();
System.out.print("Enter new movie genre: ");
String updateGenre = scanner.nextLine();
System.out.print("Enter new release year: ");
int updateYear = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new director name: ");
String updateDirector = scanner.nextLine();
updateMovie(updateId, updateTitle, updateGenre, updateYear, updateDirector);
break;
case 4:
System.out.print("Enter movie ID to delete: ");
int deleteId = scanner.nextInt();
deleteMovie(deleteId);
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
}
Explanation of the Program
This program is a simple Movie Database implemented using SQLite and Java. The program allows you to perform the following operations:
- Insert Movie: Add a new movie with details such as title, genre, release year, and director.
- List Movies: Display all the movies stored in the database.
- Update Movie: Modify the details of an existing movie using its ID.
- Delete Movie: Remove a movie from the database based on its ID.
How to Run the Program
- Ensure you have the SQLite JDBC driver added to your Java project. You can download it from the official SQLite website.
- Compile the Java file using a Java compiler, for example:
javac MovieDatabase.java
. - Run the program using the command:
java MovieDatabase
. - The program will present a menu in the console where you can choose an action (add, list, update, delete, or exit).