Download and run jOOQ in 6 easy steps:
If you haven't already downloaded them, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
Alternatively, you can create a Maven dependency:
<dependency> <groupId>org.jooq</groupId> <!-- artefacts are jooq, jooq-meta, jooq-codegen --> <artifactId>jooq</artifactId> <version>=$version?></version> </dependency>
For this example, we'll be using MySQL. If you haven't already downloaded MySQL Connector/J, download it here:
http://dev.mysql.com/downloads/connector/j/
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
CREATE DATABASE guestbook; CREATE TABLE `posts` ( `id` bigint(20) NOT NULL, `body` varchar(255) DEFAULT NULL, `timestamp` datetime DEFAULT NULL, `title` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) );
In this step, we're going to use jOOQ's command line tools to generate classes that
map to the Posts table we just created. More detailed information about how to
set up the jOOQ code generator can be found here:
http://www.jooq.org/doc/=$minorVersion?>/manual/code-generation/codegen-configuration
The easiest way to generate a schema is to copy the jOOQ jar files (there should be 3) and the MySQL Connector jar file to a temporary directory. Then, create a guestbook.xml that looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.0.0.xsd">
<!-- Configure the database connection here -->
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/guestbook</url>
<user>root</user>
<password></password>
</jdbc>
<generator>
<!-- The default code generator. You can override this one, to generate your own code style
Defaults to org.jooq.util.DefaultGenerator -->
<name>org.jooq.util.DefaultGenerator</name>
<database>
<!-- The database type. The format here is:
org.util.[database].[database]Database -->
<name>org.jooq.util.mysql.MySQLDatabase</name>
<!-- The database schema (or in the absence of schema support, in your RDBMS this
can be the owner, user, database name) to be generated -->
<inputSchema>guestbook</inputSchema>
<!-- All elements that are generated from your schema (A Java regular expression. Use the pipe to separate several expressions)
Watch out for case-sensitivity. Depending on your database, this might be important! -->
<includes>.*</includes>
<!-- All elements that are excluded from your schema (A Java regular expression. Use the pipe to separate several expressions).
Excludes match before includes -->
<excludes></excludes>
</database>
<target>
<!-- The destination package of your generated classes (within the destination directory) -->
<packageName>test.generated</packageName>
<!-- The destination directory of your generated classes -->
<directory>C:/workspace/MySQLTest/src</directory>
</target>
</generator>
</configuration>
Replace the username with whatever user has the appropriate privileges. You'll want to look at the other values and replace as necessary. Here are the two interesting properties:
generator.target.package - set this to the parent package you want
to create for the generated classes. The setting of test.generated
will cause the test.generated.Posts and
test.generated.PostsRecord to be created
generator.target.directory - the directory to output to.
Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems):
java -classpath jooq-=$version?>.jar;jooq-meta-=$version?>.jar;jooq-codegen-=$version?>.jar;mysql-connector-java-5.1.18-bin.jar;. org.jooq.util.GenerationTool /guestbook.xml
Note the prefix slash before guestbook.properies. Even though it's in our working directory, we need to prepend a slash, as it is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ =$version?> is being used. If everything has worked, you should see this in your console output:
Nov 1, 2011 7:25:06 PM org.jooq.impl.JooqLogger info INFO: Initialising properties : /guestbook.xml Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Database parameters Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: ---------------------------------------------------------- Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: dialect : MYSQL Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: schema : guestbook Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: target dir : /Users/jOOQ/Documents/workspace/MySQLTest/src Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: target package : test.generated Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: ---------------------------------------------------------- Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Emptying : /Users/jOOQ/workspace/MySQLTest/src/test/generated Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Generating classes in : /Users/jOOQ/workspace/MySQLTest/src/test/generated Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Generating schema : Guestbook.java Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Schema generated : Total: 122.18ms Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Sequences fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Masterdata tables fetched: 0 (0 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Tables fetched : 5 (5 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Generating tables : /Users/jOOQ/workspace/MySQLTest/src/test/generated/tables Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: ARRAYs fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Enums fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: UDTs fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Generating table : Posts.java Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Tables generated : Total: 680.464ms, +558.284ms Nov 1, 2011 7:25:07 PM org.jooq.impl.JooqLogger info INFO: Generating Keys : /Users/jOOQ/workspace/MySQLTest/src/test/generated/tables Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Keys generated : Total: 718.621ms, +38.157ms Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Generating records : /Users/jOOQ/workspace/MySQLTest/src/test/generated/tables/records Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Generating record : PostsRecord.java Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Table records generated : Total: 782.545ms, +63.924ms Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Routines fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: Packages fetched : 0 (0 included, 0 excluded) Nov 1, 2011 7:25:08 PM org.jooq.impl.JooqLogger info INFO: GENERATION FINISHED! : Total: 791.688ms, +9.143ms
Let's just write a vanilla main class in the project containing the generated classes:
// For convenience, always static import your generated tables and
// jOOQ functions to decrease verbosity:
import static test.generated.Tables.*;
import static org.jooq.impl.Factory.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost:3306/guestbook";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
// For the sake of this tutorial, let's keep exception handling simple
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ignore) {
}
}
}
}
}
This is pretty standard code for establishing a MySQL connection.
Let's add a simple query:
Executor create = new Executor(conn, SQLDialect.MYSQL); Result<Record> result = Executor.select().from(POSTS).fetch();
First get an instance of Factory so we can write a simple
SELECT query. We pass an instance of the MySQL connection to
Factory. Note that the factory doesn't close the connection.
We'll have to do that ourselves.
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
After the line where we retrieve the results, let's iterate over the results and print out the data:
for (Record r : result) {
Long id = r.getValue(POSTS.ID);
String title = r.getValue(POSTS.TITLE);
String description = r.getValue(POSTS.BODY);
System.out.println("ID: " + id + " title: " + title + " desciption: " + description);
}
The full program should now look like this:
package test;
// For convenience, always static import your generated tables and
// jOOQ functions to decrease verbosity:
import static test.generated.Tables.*;
import static org.jooq.impl.Factory.*;
import java.sql.*;
import org.jooq.*;
import org.jooq.impl.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost:3306/guestbook";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
Executor create = new Executor(conn, SQLDialect.MYSQL);
Result<Record> result = create.select().from(POSTS).fetch();
for (Record r : result) {
Long id = r.getValue(POSTS.ID);
String title = r.getValue(POSTS.TITLE);
String description = r.getValue(POSTS.BODY);
System.out.println("ID: " + id + " title: " + title + " desciption: " + description);
}
} catch (Exception e) {
// For the sake of this tutorial, let's keep exception handling simple
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ignore) {
}
}
}
}
}
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the manual:
http://www.jooq.org/doc/=$minorVersion?>/manual
... explore the Javadoc:
http://www.jooq.org/javadoc/latest/
... or join the news group:
https://groups.google.com/forum/#!forum/jooq-user
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/