一,下载jooq包和MySQL SQL driver
http://www.jooq.org/download.php
http://dev.mysql.com/downloads/connector/j/
二,创建MySQL数据库
将guestbook.xml文件和MySQL SQL driver(mysql-connector-java-*-bin.jar)拷贝到{jOOQ_path}\lib文件夹中,执行:
五,编写代码
参考:http://www.jooq.org/learn.php
http://www.jooq.org/download.php
http://dev.mysql.com/downloads/connector/j/
二,创建MySQL数据库
CREATE DATABASE `guestbook` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
USE guestbook ;
CREATE TABLE `posts` (
`id` BIGINT(20) NOT NULL,
`body` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_bin',
`timestamp` DATETIME NULL DEFAULT NULL,
`title` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_bin',
PRIMARY KEY (`id`)
) COLLATE='utf8_bin' ENGINE=InnoDB;
三,准备自动生成代码的配置文件 guestbook.xml
<?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://192.168.0.122:3306/guestbook</url>
<user>root</user>
<password>rootadmin</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>D:/JavaStudy/jOOQ-3.1.0-full/lib</directory>
</target>
</generator>
</configuration>
四,执行org.jooq.util.GenerationTool,生成代码。
将guestbook.xml文件和MySQL SQL driver(mysql-connector-java-*-bin.jar)拷贝到{jOOQ_path}\lib文件夹中,执行:
java -classpath jooq-3.1.0.jar;jooq-meta-3.1.0.jar;jooq-codegen-3.1.0.jar;mysql-connector-java-5.1.18-bin.jar;org.jooq.util.GenerationTool /guestbook.xml
在Eclipse中运行生成代码,可以参考:http://www.jooq.org/doc/3.1/manual/code-generation/codegen-configuration/
五,编写代码
package test.console;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import static test.generated.Tables.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
String userName = "root";
String password = "rootadmin";
String url = "jdbc:mysql://192.168.0.122:3306/guestbook";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
// delete table content
create.delete(POSTS).execute();
// insert
for (int i = 0; i < 10; i++) {
create.insertInto(POSTS, POSTS.ID, POSTS.TITLE, POSTS.BODY,POSTS.TIMESTAMP).
values(new Long(i) , "TITLE " + i, "BODY " + i,new java.sql.Timestamp(System.currentTimeMillis())).execute();
}
// select
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);
}
// delete
create.delete(POSTS).where(POSTS.ID.equal(new Long(9))).execute();
// update
create.update(POSTS)
.set(POSTS.TITLE, "===title===")
.set(POSTS.BODY, "===body===")
.where(POSTS.ID.equal(new Long(1)))
.execute();
String sql = create.update(POSTS)
.set(POSTS.TITLE, "===title===")
.set(POSTS.BODY, "===body===")
.where(POSTS.ID.equal(new Long(1)))
.getSQL();
System.out.println(sql);
} 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) {
}
}
}
}
}
参考:http://www.jooq.org/learn.php