From aca827647f86d46dd1a9dfcda3ee4689481dd766 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 12 Nov 2011 16:36:20 +0000 Subject: [PATCH] Added Ikai Lan's tutorial --- jOOQ-website/frame.php | 3 + jOOQ-website/manual-single-page/index.php | 2 +- .../manual/META/Configuration/index.php | 2 +- jOOQ-website/src/main/resources/manual.xml | 2 +- jOOQ-website/tutorial.php | 350 ++++++++++++++++++ 5 files changed, 356 insertions(+), 3 deletions(-) create mode 100644 jOOQ-website/tutorial.php diff --git a/jOOQ-website/frame.php b/jOOQ-website/frame.php index 2136f63810..2f42307f9e 100644 --- a/jOOQ-website/frame.php +++ b/jOOQ-website/frame.php @@ -32,6 +32,9 @@ + diff --git a/jOOQ-website/manual-single-page/index.php b/jOOQ-website/manual-single-page/index.php index 8b5c87f1ec..2928c78d44 100644 --- a/jOOQ-website/manual-single-page/index.php +++ b/jOOQ-website/manual-single-page/index.php @@ -1469,7 +1469,7 @@ public void bind(BindContext context) throws SQLException; #Configure the database connection here jdbc.Driver=com.mysql.jdbc.Driver jdbc.URL=jdbc:mysql://[your jdbc URL] -jdbc.Schema=[your database schema] +jdbc.Schema=[your database schema / owner / name] jdbc.User=[your database user] jdbc.Password=[your database password] diff --git a/jOOQ-website/manual/META/Configuration/index.php b/jOOQ-website/manual/META/Configuration/index.php index db3538d3ec..4647fe39e0 100644 --- a/jOOQ-website/manual/META/Configuration/index.php +++ b/jOOQ-website/manual/META/Configuration/index.php @@ -74,7 +74,7 @@ function printContent() { #Configure the database connection here jdbc.Driver=com.mysql.jdbc.Driver jdbc.URL=jdbc:mysql://[your jdbc URL] -jdbc.Schema=[your database schema] +jdbc.Schema=[your database schema / owner / name] jdbc.User=[your database user] jdbc.Password=[your database password] diff --git a/jOOQ-website/src/main/resources/manual.xml b/jOOQ-website/src/main/resources/manual.xml index 0cca3bb501..22abdebfa5 100644 --- a/jOOQ-website/src/main/resources/manual.xml +++ b/jOOQ-website/src/main/resources/manual.xml @@ -1285,7 +1285,7 @@ public void bind(BindContext context) throws SQLException; #Configure the database connection here jdbc.Driver=com.mysql.jdbc.Driver jdbc.URL=jdbc:mysql://[your jdbc URL] -jdbc.Schema=[your database schema] +jdbc.Schema=[your database schema / owner / name] jdbc.User=[your database user] jdbc.Password=[your database password] diff --git a/jOOQ-website/tutorial.php b/jOOQ-website/tutorial.php new file mode 100644 index 0000000000..d8f7aa853a --- /dev/null +++ b/jOOQ-website/tutorial.php @@ -0,0 +1,350 @@ + +

Introduction

+

+Download and run jOOQ in 6 easy steps: +

+ + + +

Preparation: Download jOOQ and your SQL driver

+

+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>1.6.9</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/ +

+ + +

Step 1: Create a SQL database and a table

+

+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`)
+);
+
+ +

Step 2: Generate classes

+

+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/manual/META/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.properties +that looks like this: +

+
+#Configure the database connection here
+jdbc.Driver=com.mysql.jdbc.Driver
+jdbc.URL=jdbc:mysql://localhost:3306/guestbook
+jdbc.Schema=guestbook
+jdbc.User=root
+jdbc.Password=
+
+#The default code generator. You can override this one, to generate your own code style
+#Defaults to org.jooq.util.DefaultGenerator
+generator=org.jooq.util.DefaultGenerator
+
+#The database type. The format here is:
+#generator.database=org.util.[database].[database]Database
+generator.database=org.jooq.util.mysql.MySQLDatabase
+
+#All elements that are generated from your schema (several Java regular expressions, separated by comma)
+#Watch out for case-sensitivity. Depending on your database, this might be important!
+generator.database.includes=.*
+
+#All elements that are excluded from your schema (several Java regular expressions, separated by comma). Excludes match before includes
+generator.database.excludes=
+
+#Primary key / foreign key relations should be generated and used. 
+#This will be a prerequisite for various advanced features
+#Defaults to false
+generator.generate.relations=true
+
+#Generate deprecated code for backwards compatibility 
+#Defaults to true
+generator.generate.deprecated=false
+
+#The destination package of your generated classes (within the destination directory)
+generator.target.package=test.generated
+
+#The destination directory of your generated classes
+generator.target.directory=C:/workspace/MySQLTest/src
+
+

+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.properties in your temp directory, type this +(use colons instead of semi-colons on UNIX/Linux systems): +

+
+java -classpath jooq-1.6.9.jar;jooq-meta-1.6.9.jar;jooq-codegen-1.6.9.jar;mysql-connector-java-5.1.18-bin.jar;. org.jooq.util.GenerationTool /guestbook.properties
+
+

+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 1.6.9 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.properties
+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: Generating factory       : GuestbookFactory.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
+
+ + + +

Step 3: Write a main class and establish a MySQL connection

+

+Let's just write a vanilla main class in the project containing the generated classes: +

+
+public class Main {
+    public static void main(String[] args) throws Exception {
+        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) {
+            e.printStackTrace();
+        } finally {
+            if (conn != null) {
+                conn.close();
+            }
+        }
+    }
+}
+
+

+This is pretty standard code for establishing a MySQL connection. +

+ + +

Step 4: Write a query using jOOQ's DSL

+

+Let's add a simple query: +

+
+GuestbookFactory create = new GuestbookFactory(conn);
+Result<?> result = create.select().from(Posts.POSTS).fetch();
+
+

+First get an instance of GuestbookFactory so we can write a simple +SELECT query. We pass an instance of the MySQL connection to +GuestbookFactory. 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. +

+ + +

Step 5: Iterate over results

+

+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;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import org.jooq.Record;
+import org.jooq.Result;
+
+import test.generated.GuestbookFactory;
+import test.generated.tables.Posts;
+
+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);
+
+            GuestbookFactory create = new GuestbookFactory(conn);
+            Result result = create.select().from(Posts.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) {
+            e.printStackTrace();
+        } finally {
+            if (conn != null) {
+                conn.close();
+            }
+        }
+    }
+}
+
+ +

Step 6: Explore!

+

+jOOQ has grown to be a comprehensive SQL library. For more information, please consider the manual:
+http://www.jooq.org/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/ +

+ + \ No newline at end of file