From 176711422333a2189ec3061920757a8635b56cc4 Mon Sep 17 00:00:00 2001 From: "Jefferson C. Bernard" <45987542+jbernar3@users.noreply.github.com> Date: Mon, 2 Mar 2020 09:51:02 -0500 Subject: [PATCH] Update Database.java --- .../edu/brown/cs/student/main/Database.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/brown/cs/student/main/Database.java b/src/main/java/edu/brown/cs/student/main/Database.java index c8be0c2..6d11bd5 100644 --- a/src/main/java/edu/brown/cs/student/main/Database.java +++ b/src/main/java/edu/brown/cs/student/main/Database.java @@ -21,6 +21,8 @@ public class Database { +// String urlToDB = "jdbc:sqlite:" + "data/data.sqlite3"; +// Connection conn=DriverManager.getConnection(urlToDB); private static Connection conn = null; private static List words = new ArrayList<>(); @@ -36,6 +38,27 @@ public class Database { * TODO: Initialize the database connection, turn foreign keys on, * and then create the word and corpus tables if they do not exist. */ + + // this line loads the driver manager class, and must be + // present for everything else to work properly +// Class.forName("org.sqlite.JDBC"); + String urlToDB = "jdbc:sqlite:" + filename; + conn=DriverManager.getConnection(urlToDB); + // these two lines tell the database to enforce foreign keys during operations, and should be present + Statement stat = conn.createStatement(); + stat.executeUpdate("PRAGMA foreign_keys=ON;"); + stat.close(); + + PreparedStatement prep1; + prep1 = conn.prepareStatement("CREATE TABLE IF NOT EXISTS corpus (id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT);"); + prep1.executeUpdate(); + prep1.close(); + + PreparedStatement prep2; + prep2 = conn.prepareStatement("CREATE TABLE IF NOT EXISTS word "+ + "(corpus_id INTEGER, word TEXT, FOREIGN KEY (corpus_id) REFERENCES corpus(id) ON DELETE CASCADE);"); + prep2.executeUpdate(); + prep2.close(); } @@ -46,9 +69,10 @@ public class Database { * @throws SQLException if something goes wrong with a SQL query. */ void readCorpus(String filename) throws SQLException, ClassNotFoundException { - - PreparedStatement prep = - conn.prepareStatement("SELECT * from corpus WHERE filename=?"); + if (conn == null) { + System.out.println("suck my gpa"); + } + PreparedStatement prep = conn.prepareStatement("SELECT * from corpus WHERE filename=?"); prep.setString(1, filename); ResultSet rs = prep.executeQuery(); if (rs.isClosed()) { @@ -117,7 +141,8 @@ void readAll() throws SQLException { Map getFrequencyMap() throws SQLException { Map freqMap = new HashMap<>(); //TODO: select all filenames and how many words are associated with those filenames from the database - PreparedStatement prep = conn.prepareStatement("");//Your SQL here! + PreparedStatement prep = conn.prepareStatement( + "SELECT filename, COUNT(*) FROM corpus, word WHERE word.corpus_id = corpus.id GROUP BY filename");//Your SQL here! ResultSet rs = prep.executeQuery(); while (rs.next()) { freqMap.put(rs.getString(1), rs.getInt(2)); @@ -138,7 +163,8 @@ Map getFrequencyMap() throws SQLException { Map getInstanceMap() throws SQLException { Map instMap = new HashMap<>(); //TODO: select the five most common words from the entire database, and how many times they appear - PreparedStatement prep = conn.prepareStatement("");//Your SQL Here! + PreparedStatement prep = conn.prepareStatement( + "SELECT word, COUNT(*) FROM word GROUP BY word ORDER BY COUNT(*) DESC LIMIT 5;");//Your SQL Here! ResultSet rs = prep.executeQuery(); while (rs.next()) { instMap.put(rs.getString(1), rs.getInt(2)); @@ -158,4 +184,3 @@ public List getWords() { } } -