Java Reference
In-Depth Information
At this point, we have still only parsed the content. We still have to load it to the database. To do
this, we will have a TextDatabase. It will have a method that will configure the database schema and
another that will load the database given a collection of ShakespeareText. This, of course, will delegate
immediately to a TextDatabaseTextLoader instance, which is responsible for loading a text. It will delegate
to a TextDatabaseLineLoader instance, which is responsible for loading a line. It, in turn, will delegate to a
TextDabaseWordLoader instance, which is responsible for loading the word. And then it will all be loaded!
The code for all these classes (along with a Main class to test it) is given in Listing A-3.
Listing A-3. Object-Oriented Database Loading
### Main.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class Main {
public static void printDatabaseSizing(Connection conn) throws SQLException {
System.out.println("SIZES");
System.out.println("-----");
Statement stmt = conn.createStatement();
for (String table : new String[]{"\"text\"", "line", "word", "line_word"})
{
try (ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + table)) {
boolean hasNext = rs.next();
assert hasNext : "No result in count from table " + table;
System.out.println(table + " => " + rs.getInt(1));
}
}
}
public static ResultSet queryResults(Connection conn) throws SQLException {
return conn.createStatement().executeQuery(
"SELECT t.name, l.\"offset\", w.\"value\", lw.\"offset\" " +
"FROM \"text\" t, word w " +
"INNER JOIN line l ON (t.id = l.text_id) " +
"INNER JOIN line_word lw ON (" +
"lw.line_id = l.id AND lw.word_id = w.id" +
")"
);
}
public static void printWordUsages(Connection conn) throws SQLException {
int lineNumber = 0;
try (ResultSet rs = queryResults(conn)) {
String lastText = null;
int lastLine = -1;
Search WWH ::




Custom Search