Skip to content

Instantly share code, notes, and snippets.

@takaki
Last active July 13, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takaki/3e014e5bb45ea38b7b489f8bc3b84e39 to your computer and use it in GitHub Desktop.
Save takaki/3e014e5bb45ea38b7b489f8bc3b84e39 to your computer and use it in GitHub Desktop.
jooq {
library (sourceSets.main) {
jdbc {
driver = "org.sqlite.JDBC"
url = 'jdbc:sqlite:./library.db'
}
generator {
target {
packageName = 'example.jooq'
}
}
}
}
$ sqlite library.db
sqlite> CREATE TABLE `author` (
`id` int NOT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);
sqlite> INSERT INTO "author" VALUES(1,'Hoge','Foo');
sqlite> INSERT INTO "author" VALUES(2,'Bar','Baz');
$ gradle generateLibraryJooqSchemaSource
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import java.sql.Connection;
import java.sql.DriverManager;
import static example.jooq.Tables.AUTHOR;
public class Main {
public static void main(final String[] args) {
final String userName = "";
final String password = "";
final String url = "jdbc:sqlite:library.db";
try (Connection conn = DriverManager
.getConnection(url, userName, password);
final DSLContext create = DSL.using(conn, SQLDialect.MYSQL)) {
final Result<Record> result = create.select().from(AUTHOR).fetch();
for (final Record r : result) {
final Integer id = r.getValue(AUTHOR.ID);
final String firstName = r.getValue(AUTHOR.FIRST_NAME);
final String lastName = r.getValue(AUTHOR.LAST_NAME);
System.out.println(
"ID: " + id + " first name: " + firstName + " last name: " + lastName);
}
} catch (final Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment