|
|
|
@ -1,12 +1,48 @@ |
|
|
|
|
import java.sql.*; |
|
|
|
|
import java.util.Scanner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DisplayAuthors { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String... args) { |
|
|
|
|
System.out.println("What would you like to do:\n1. Print all authors\n2. Select all books by a specific author\nOther: quit."); |
|
|
|
|
Scanner scanner = new Scanner(System.in); |
|
|
|
|
int choice = scanner.nextInt(); |
|
|
|
|
if (choice != 1 && choice != 2) { |
|
|
|
|
System.out.println("You did not choose to print authors or books, exiting."); |
|
|
|
|
} else { |
|
|
|
|
handleDatabase(choice); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void handleDatabase(int choice) { |
|
|
|
|
|
|
|
|
|
final String DATABASE_URL = "jdbc:derby:books"; |
|
|
|
|
final String SELECT_QUERY = |
|
|
|
|
"SELECT authorID, firstName, lastName, FROM authors"; |
|
|
|
|
String SELECT_QUERY; |
|
|
|
|
int id = 0; |
|
|
|
|
if (choice == 1) { |
|
|
|
|
SELECT_QUERY = |
|
|
|
|
"SELECT authorID, firstName, lastName FROM authors"; |
|
|
|
|
} else { |
|
|
|
|
System.out.println("Please enter the author ID for the author whose books you want: "); |
|
|
|
|
id = new Scanner(System.in).nextInt(); //One-liner for reading input :D
|
|
|
|
|
SELECT_QUERY = |
|
|
|
|
"SELECT title, copyright, titles.isbn FROM titles\n" + |
|
|
|
|
"JOIN authorISBN ON authorISBN.isbn = titles.isbn\n" + |
|
|
|
|
"JOIN authors ON authors.authorID = authorISBN.authorID WHERE authors.authorID = " + id; |
|
|
|
|
|
|
|
|
|
//This could have been made way shorter using a subquery by Apache Derby is stupid and didn't like it.
|
|
|
|
|
//I don't know why anyone would ever want to use a RDBM written in pure Java. Aside from very specific
|
|
|
|
|
//Embedded cases or something, you should use SQLite.
|
|
|
|
|
/* |
|
|
|
|
SELECT titles.title, titles.copyright, titles.isbn FROM titles GROUP BY isbn |
|
|
|
|
HAVING isbn in (SELECT isbn FROM authorISBN WHERE authorID = 1) |
|
|
|
|
|
|
|
|
|
WHY DOESN'T THAT ^ WORK >:( |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try (Connection conn = DriverManager.getConnection( |
|
|
|
|
DATABASE_URL, "deitel", "deitel"); |
|
|
|
@ -16,8 +52,11 @@ public class DisplayAuthors { |
|
|
|
|
ResultSetMetaData metaData = resultSet.getMetaData(); |
|
|
|
|
int numberOfColumns = metaData.getColumnCount(); |
|
|
|
|
|
|
|
|
|
System.out.printf("Authors Table of Books Database:%n%n"); |
|
|
|
|
|
|
|
|
|
if (choice == 1) { |
|
|
|
|
System.out.printf("Authors Table of Books Database:%n%n"); |
|
|
|
|
} else { |
|
|
|
|
System.out.printf("Books written by author with ID: " + id + "%n%n"); |
|
|
|
|
} |
|
|
|
|
for (int i = 1; i <= numberOfColumns; i++) |
|
|
|
|
System.out.printf("%-8s\t", metaData.getColumnName(i)); |
|
|
|
|
System.out.println(); |
|
|
|
@ -32,7 +71,6 @@ public class DisplayAuthors { |
|
|
|
|
} catch (SQLException sqlException) { |
|
|
|
|
sqlException.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|