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
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 {
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 =;
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, l.\"offset\", w.\"value\", lw.\"offset\" " +
"FROM \"text\" t, word w " +
"INNER JOIN line l ON ( = l.text_id) " +
"INNER JOIN line_word lw ON (" +
"lw.line_id = AND lw.word_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