Initial commit

master
Noah Pederson 6 years ago
parent 1dfbfa8c7c
commit 015f3067b7
  1. 2
      .idea/misc.xml
  2. 10
      Assessment-7/Assessment-7.iml
  3. 50
      Assessment-7/src/DisplayAuthors.java

@ -13,7 +13,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="Java 8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -7,5 +7,15 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://C:/Program Files/Java/jdk1.8.0_66/db/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://C:/Program Files/Java/jdk1.8.0_66/db/lib" recursive="false" />
</library>
</orderEntry>
</component>
</module>

@ -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();
}
}
}
}

Loading…
Cancel
Save