data manipulation and query building using jooq
Last active
January 20, 2016 19:29
-
-
Save mizanRahman/f6e0b4b004368eb91a0a to your computer and use it in GitHub Desktop.
jooq demo
This file contains hidden or 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
| package com.jooq.demo; | |
| 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 java.sql.SQLException; | |
| import static org.jooq.impl.DSL.field; | |
| import static org.jooq.impl.DSL.table; | |
| /** | |
| * Created by mac on 1/20/16. | |
| */ | |
| public class Application { | |
| public static void main(String[] args) { | |
| Connection connection = Connect("root", "root", | |
| "jdbc:mysql://192.168.99.100:3306/spring_demo"); | |
| DSLContext create = DSL.using(connection, SQLDialect.MYSQL); | |
| printCards(create); | |
| buildQueries(create); | |
| } | |
| private static void buildQueries(DSLContext create) { | |
| String sql = create.select() | |
| .from("cards") | |
| .where(field("pan").equal(field("12"))) | |
| .and(field("id").equal(field("1"))) | |
| .getSQL(); | |
| System.out.println(sql); | |
| String sql2 = create.select(field("BOOK.TITLE"), field("AUTHOR.FIRST_NAME"), field("AUTHOR.LAST_NAME")) | |
| .from(table("BOOK")) | |
| .join(table("AUTHOR")) | |
| .on(field("BOOK.AUTHOR_ID").equal(field("AUTHOR.ID"))) | |
| .where(field("BOOK.PUBLISHED_IN").equal(1948)) | |
| .getSQL(); | |
| System.out.println(sql2); | |
| } | |
| private static void printCards(DSLContext create) { | |
| Result<Record> result = create.select().from(table("cards")).fetch(); | |
| for (Record r : result) { | |
| String s = new StringBuilder() | |
| .append("id=").append(r.getValue("id")) | |
| .append(" ").append("pan=").append(r.getValue("pan")) | |
| .toString(); | |
| System.out.println(s); | |
| } | |
| } | |
| private static Connection Connect(String userName, String password, String url) { | |
| try { | |
| Connection conn = DriverManager.getConnection(url, userName, password); | |
| return conn; | |
| } catch (SQLException e) { | |
| throw new RuntimeException("can not connect to database", e); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| group 'org.jooq' | |
| version '1.0-SNAPSHOT' | |
| apply plugin: 'java' | |
| apply plugin: 'application' | |
| mainClassName = "com.jooq.demo.Application" | |
| sourceCompatibility = 1.5 | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| compile "org.jooq:jooq:3.7.2" | |
| compile "org.jooq:jooq-meta:3.7.2" | |
| compile "org.jooq:jooq-codegen:3.7.2" | |
| runtime 'mysql:mysql-connector-java:5.1.6' | |
| testCompile group: 'junit', name: 'junit', version: '4.11' | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment