Last active
July 13, 2016 12:25
-
-
Save takaki/3e014e5bb45ea38b7b489f8bc3b84e39 to your computer and use it in GitHub Desktop.
jOOQをGradleで設定する ref: http://qiita.com/takaki@github/items/be4c4e550e73dec113b1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jooq { | |
library (sourceSets.main) { | |
jdbc { | |
driver = "org.sqlite.JDBC" | |
url = 'jdbc:sqlite:./library.db' | |
} | |
generator { | |
target { | |
packageName = 'example.jooq' | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gradle generateLibraryJooqSchemaSource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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