14. Using the JDBC API and any relational database (e.g.
H2)
make the following queries:
import [Link].*;
public class Main {
public static void main(String[] args) {
try {
String url = "jdbc:h2:mem:test";
try (Connection conn = [Link](url);
Statement stmt = [Link]()) {
// Drop the table if it already exists
[Link]("DROP TABLE IF EXISTS MOVIES");
// Create the MOVIES table
[Link]("CREATE TABLE MOVIES (id INTEGER
AUTO_INCREMENT PRIMARY KEY, " +
"title VARCHAR(255), genre VARCHAR(255),
yearOfRelease INTEGER)");
// Insert three records into the MOVIES table
PreparedStatement insertStmt = [Link](
"INSERT INTO MOVIES (title, genre, yearOfRelease)
VALUES (?, ?, ?)");
[Link](1, "The Shawshank Redemption");
[Link](2, "Drama");
[Link](3, 1994);
[Link]();
[Link](1, "Inception");
[Link](2, "Sci-Fi");
[Link](3, 2010);
[Link]();
[Link](1, "The Godfather");
[Link](2, "Crime");
[Link](3, 1972);
[Link]();
// Update a record in the MOVIES table
PreparedStatement updateStmt = [Link](
"UPDATE MOVIES SET genre = ? WHERE title = ?");
[Link](1, "Crime, Drama");
[Link](2, "The Shawshank Redemption");
[Link]();
// Delete a record from the MOVIES table
PreparedStatement deleteStmt = [Link](
"DELETE FROM MOVIES WHERE id = ?");
[Link](1, 3);
[Link]();
// Display all other records in the MOVIES table
ResultSet resultSet = [Link]("SELECT * FROM
MOVIES");
while ([Link]()) {
[Link]("ID: " + [Link]("id") +
", Title: " + [Link]("title") +
", Genre: " + [Link]("genre") +
", Year of Release: " +
[Link]("yearOfRelease"));
} catch (SQLException e) {
[Link]();