Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package osu.cse 3 2 4 1 . options; import java.sql . Connection; import java.sql . PreparedStatement; import java.sql . ResultSet; import java.sql . SQLException; import

package osu.cse3241.options;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import osu.cse3241.GRS;
import osu.cse3241.utilities.Utilities;
public class SearchArtistTrack {
private static Set MENU_OPTIONS = new HashSet<>(Arrays.asList('1','2','x'));
public static void menu(Scanner cin){
Utilities.printDivider();
System.out.print("SEARCH:
"
+"1. Artists
"
+"2. Tracks
"
+ "Input numerical selection (or 'x' to quit): ");
String input = cin.nextLine();
char selection =!input.isEmpty()? input.charAt(0) : '';
while (!MENU_OPTIONS.contains(selection)){
System.out.print("Incorrect option specified! Try again: ");
input = cin.nextLine();
selection =!input.isEmpty()? input.charAt(0) : '';
}
switch (selection){
case '1':
searchArtists(cin);
break;
case '2':
searchTracks(cin);
break;
case 'x':
System.out.println("Exiting...");
System.exit(0);
break;
default:
break;
}
}
/**
* Search tracks in the database according to input track name.
*
* @param cin scanner
*/
private static void searchTracks(Scanner cin){
System.out.print("Input track name (or 'x' to quit): ");
String trackName = cin.nextLine();
if (!trackName.equals("x") && !trackName.trim().isEmpty()){
String sql = "SELECT track_name FROM tracks WHERE track_name LIKE ?";
try (Connection conn = GRS.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)){
pstmt.setString(1,"%"+ trackName +"%");
ResultSet rs = pstmt.executeQuery();
System.out.println("Matching tracks:");
while (rs.next()){
System.out.println(rs.getString("track_name"));
}
} catch (SQLException e){
System.out.println("Error executing SQL query: "+ e.getMessage());
}
}
}
/**
* Search artists in the database according to input artist name.
*
* @param cin scanner
*/
private static void searchArtists(Scanner cin){
Utilities.placeholder();
/* TODO: Implement searching artists by artist name */
/* Example:
System.out.print("Input artist name (or 'x' to quit): ");
String artistName = cin.nextLine();
if (!artistName.equals("x") && !artistName.trim().isEmpty()){
String sql = "SELECT artist_name FROM artists WHERE artist_name LIKE ?";
try (Connection conn = GRS.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)){
pstmt.setString(1,"%"+ artistName +"%");
ResultSet rs = pstmt.executeQuery();
System.out.println("Matching artists:");
while (rs.next()){
System.out.println(rs.getString("artist_name"));
}
} catch (SQLException e){
System.out.println("Error executing SQL query: "+ e.getMessage());
}
}
*/
}
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
while (true){
menu(cin);
}
}
fill in the todo

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

What is the main commercial use of limestone or calcite?

Answered: 1 week ago

Question

why is the CGEIT certification important?

Answered: 1 week ago

Question

All of the following phrases are redundant EXCEPT _ _ _ _ _ _ _ _ .

Answered: 1 week ago