I want to create .java from table using jooq programmaticly not by .xml.

I have already tried by xml but it is not what i want.

Firstly is it possible to do by jooq?

Secondly does anybody know how to do it?

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.util.DefaultGenerator;
import org.jooq.util.JavaGenerator;
import org.jooq.util.mysql.MySQLDatabase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
    public static void main(String[] args) {
        Connection conn = null;

        String userName = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/library";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);

            DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
            Result<Record> result = create.select().from("AUTHOR").fetch();

            //------------ here I want to create AUTHOR.java from table AUTHOR by connecting to database  
            DefaultGenerator  g = new DefaultGenerator();
            MySQLDatabase database = new MySQLDatabase();
            database.getSchema(conn.getSchema());
            JavaGenerator javaGenerator = new JavaGenerator();
            javaGenerator.generate(database);

            //------------

            for (Record r : result) {
                Long id = r.getValue(AUTHOR.ID);
                String firstName = r.getValue(AUTHOR.FIRST_NAME);
                String lastName = r.getValue(AUTHOR.LAST_NAME);

                System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ignore) {
                }
            }
        }
    }
}
有帮助吗?

解决方案

Yes, you can programmatically configure jOOQ's code generator. When you look at GenerationTool's source code, you'll see that you can call its overloaded main() method either from the console (as documented in the manual), or by passing it an org.jooq.util.jaxb.Configuration object. An example:

import org.jooq.util.jaxb.*;

// [...]

Configuration configuration = new Configuration()
    .withJdbc(new Jdbc()
        .withDriver("com.mysql.jdbc.Driver")
        .withUrl("jdbc:mysql://localhost:3306/library")
        .withUser("root")
        .withPassword("root"))
    .withGenerator(new Generator()
        .withName("org.jooq.util.DefaultGenerator")
        .withDatabase(new Database()
            .withName("org.jooq.util.mysql.MySQLDatabase")
            .withIncludes(".*")
            .withExcludes("")
            .withInputSchema("library"))
        .withTarget(new Target()
            .withPackageName("org.jooq.util.maven.example")
            .withDirectory("target/generated-sources/jooq")));

GenerationTool.main(configuration);

The above configuration POJOs are generated by XJC, so you can use exactly the same structure as with the XML configuration.

This is now also documented in the manual.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top