Skip to content

Instantly share code, notes, and snippets.

@fge
Created March 16, 2015 19:59
Show Gist options
  • Select an option

  • Save fge/d5eb83a78046b76440e3 to your computer and use it in GitHub Desktop.

Select an option

Save fge/d5eb83a78046b76440e3 to your computer and use it in GitHub Desktop.
public final class Example
{
private static final String DB_PROPERTIES = "/db.properties";
private static final String DB_URL;
private static final String DB_USERNAME;
private static final String DB_PASSWORD;
static {
final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
final InputStream in
= Example.class.getResourceAsStream(DB_PROPERTIES);
if (in == null)
throw new ExceptionInInitializerError(DB_PROPERTIES + " not found");
try (
final InputStream inref = in;
final InputStreamReader reader = new InputStreamReader(in, decoder);
) {
final Properties properties = new Properties();
properties.load(reader);
DB_URL = properties.getProperty("db.url");
DB_USERNAME = properties.getProperty("db.username");
DB_PASSWORD = properties.getProperty("db.password");
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
if (DB_URL == null
|| DB_USERNAME == null
|| DB_PASSWORD == null)
throw new ExceptionInInitializerError();
}
private static final List<String> MAKEVALUES = Arrays.asList(
"BMW",
"Audi",
"Lotus",
"Peugeot",
"Saab",
"Toyota"
);
public static void main(final String... args)
throws SQLException, ClassNotFoundException
{
final DSLContext jooq = getJooq();
MAKEVALUES.stream().forEach(make -> {
final MakeRecord record = jooq.newRecord(Tables.MAKE);
record.setMake(make);
record.store();
});
}
private static DSLContext getJooq()
throws SQLException, ClassNotFoundException
{
final Connection connection
= DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
return DSL.using(connection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment