Java Reference
In-Depth Information
public static void createTables(final Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE word " +
"(id INT PRIMARY KEY AUTO_INCREMENT, \"value\" VARCHAR UNIQUE)"
);
stmt.execute("CREATE TABLE line_word " +
"(id INT PRIMARY KEY AUTO_INCREMENT, line_id INT, word_id INT, " +
"\"offset\" INT)"
);
}
}
public void insertWord(final int lineId, final ShakespeareWord word)
throws SQLException
{
int wordId = determineWordId(word.getWord());
createLineWord.setInt(1, lineId);
createLineWord.setInt(2, wordId);
createLineWord.setInt(3, word.getLinePosition());
boolean createdLineWord = createLineWord.executeUpdate() == 1;
assert createdLineWord : "Could not create line-word";
}
private int determineWordId(String word) throws SQLException {
if(wordIds.containsKey(word)) {
return wordIds.get(word);
} else {
int wordId = insertWordRecord(word);
wordIds.put(word, wordId);
return wordId;
}
}
private int insertWordRecord(String word) throws SQLException {
createWord.setString(1, word);
boolean createdWord = createWord.executeUpdate() == 1;
assert createdWord : "Could not create word: " + word;
try(ResultSet rs = createWord.getGeneratedKeys()) {
boolean hasWord = rs.next();
assert hasWord : "Created word but still could not find it!";
return rs.getInt(1);
}
}
public void close() throws SQLException {
createWord.close();
createLineWord.close();
}
}
Search WWH ::




Custom Search