Java Reference
In-Depth Information
private int insertLineRecord(final int textId, final int textPosition)
throws SQLException
{
createLine.setInt(1, textId);
createLine.setInt(2, textPosition);
boolean createdLine = createLine.executeUpdate() == 1;
assert createdLine : "Could not create line";
try (ResultSet rs = createLine.getGeneratedKeys()) {
boolean hasNext = rs.next();
assert hasNext :
"No result when getting generated keys for line in text id " +
textId + ", " + "offset " + textPosition;
return rs.getInt(1);
}
}
public void close() throws SQLException {
createLine.close();
wordLoader.close();
}
}
### TextDatabaseWordLoader.java
import java.sql.*;
import java.util.*;
/**
* Responsible for loading words into the database
*/
public class TextDatabaseWordLoader implements AutoCloseable {
private final PreparedStatement createWord;
private final PreparedStatement createLineWord;
private final Map<String, Integer> wordIds = new HashMap<>();
public TextDatabaseWordLoader(final Connection conn) throws SQLException {
Objects.requireNonNull(conn, "connection");
createWord = conn.prepareStatement(
"INSERT INTO word (\"value\") VALUES (?)",
Statement.RETURN_GENERATED_KEYS
);
createLineWord = conn.prepareStatement(
"INSERT INTO line_word " +
" (line_id, word_id, \"offset\") " +
" VALUES (?,?,?)",
Statement.NO_GENERATED_KEYS
);
}
Search WWH ::




Custom Search