Question: switch ( choice ) { Question 2 You are provided with a SQL file named data.sql : here is it - - Connect to or

switch (choice){ Question 2
You are provided with a SQL file named data.sql : here is it -- Connect to or create the CarDB database
CONNECT 'jdbc:derby:CarDB;create=true';
-- Drop the table if it already exists to avoid conflicts
DROP TABLE Cars;
-- Create the Cars table
CREATE TABLE Cars (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
make VARCHAR(50),
model VARCHAR(50),
manufacture_year INT,
price DECIMAL(10,2)
);
-- Insert 30 sample records into the Cars table
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Toyota', 'Corolla', 2020,20000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Ford', 'Mustang', 2018,30000.50);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Honda', 'Civic', 2019,22000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Chevrolet', 'Camaro', 2021,35000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('BMW','3 Series', 2017,28000.75);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Audi','A4',2016,27000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Mercedes-Benz', 'C-Class', 2019,40000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Tesla', 'Model 3',2021,50000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Hyundai', 'Elantra', 2018,19000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Kia', 'Optima', 2020,21000.50);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Volkswagen', 'Jetta', 2021,23000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Nissan', 'Altima', 2019,24000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Mazda', 'Mazda3',2017,18000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Subaru', 'Impreza', 2020,26000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Jeep', 'Wrangler', 2018,32000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Dodge', 'Charger', 2019,29000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Volvo','S60',2017,27000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Porsche', 'Cayenne', 2021,80000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Ferrari','488 Spider', 2020,280000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Lamborghini', 'Huracan', 2021,300000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Bentley', 'Continental GT',2020,220000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Rolls-Royce', 'Phantom', 2021,450000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Ford', 'Explorer', 2019,35000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Chevrolet', 'Tahoe', 2020,60000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('BMW','X5',2021,65000.00);
INSERT INTO Cars (make, model, manufacture_year, price)
VALUES ('Mercedes-Benz', 'GLE', 2020,70000.00);
INSERTthat contains SQL state-
ments to create and populate a Cars table.
Your Task:
You are required to write a Java program that connects to the CarShop database
and presents a menu to the user. The program should use a SELECT * FROM
CARS query to retrieve all records from the Cars table and perform tasks by
processing the ResultSet.
The following code is provided:
public class CarShopApp {
public static void main(String[] args){
// Establish connection to the database
// Create a Statement object
// Execute the SELECT * FROM CARS query
// Initialize Scanner for user input
Scanner scanner = new Scanner(
System.in);
try {
boolean running = true;
while (running){
// Display menu
System.out.printIn("Menu: ");
System.out.printIn("1. Check Stock");
System.out.printIn("2. Exit");
System.out. print("Choose an option: ");
int choice = scanner.nextInt(); The function should prompt the user to enter a manufacture year.
The function should output the number of cars available for that year.
You are only allowed to use the SELECT * FROM CARS query; you may
not use any other SQL statements to accomplish the task.
All logic should be implemented by working with the ResultSet obtained
from the SELECT * query.
Instructions:
Complete the missing code in the main method to establish the database
connection and execute the query.
Implement the checkStockByYear function within the provided skeleton
code.
Ensure your code is properly formatted and includes necessary error
handling.
Test your program to verify that it works correctly.
Submission:
Submit the complete Java code including the implemented checkStockByYear
function and the completed main method.
switch ( choice ) { Question 2 You are provided

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!