Java Reference
In-Depth Information
Listing A-4. Post-Functional Database Loading
### Main.java
import java.sql.*;
import java.util.*;
public class Main {
public static void printDatabaseSizing(Database database)
throws SQLException
{
System.out.println("SIZES");
System.out.println("-----");
try (Connection conn = database.getConnection()) {
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(Database database) throws SQLException {
int lineNumber = 0;
try (Connection conn = database.getConnection()) {
try (ResultSet rs = queryResults(conn)) {
String lastText = null;
int lastLine = -1;
while (rs.next()) {
if (lineNumber % 20 == 0) {
System.out.println("text\tline-offset\tword\tword-offset");
System.out.println("----\t-----------\t----\t-----------");
}
 
Search WWH ::




Custom Search