Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/main/java/edu/brown/cs/student/main/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> words = new ArrayList<>();

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


Expand All @@ -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()) {
Expand Down Expand Up @@ -117,7 +141,8 @@ void readAll() throws SQLException {
Map<String, Integer> getFrequencyMap() throws SQLException {
Map<String, Integer> 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));
Expand All @@ -138,7 +163,8 @@ Map<String, Integer> getFrequencyMap() throws SQLException {
Map<String, Integer> getInstanceMap() throws SQLException {
Map<String, Integer> 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));
Expand All @@ -158,4 +184,3 @@ public List<String> getWords() {
}
}