How to create java pojo from a table using jooq and not xml?

I want to create .java from a table using jooq programmatically not through .xml.

I already tried xml, but that is not what I want.

First, is it possible to do jooq?

Secondly, does anyone know how to do this?

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) {
                }
            }
        }
    }
}
+3
source share
1 answer

Yes, you can programmatically configure the jOOQ code generator. When you look at the GenerationToolsource code, you will see that you can call its overloaded method main()either from the console ( as described in the manual ), or by passing an object to it org.jooq.util.jaxb.Configuration. 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 POJO configurations are generated by XJC, so you can use the same structure as in the XML configuration.

.

+3

All Articles