Teja Teja - 1 month ago 18
MySQL Question

Jooq insert statement without code generation

I am using jOOQ to generate SQL statements. How can I create an insert statement? I am able to generate a select statement, but I am unable to generate an insert statement. I can create a select statement like this:

String selectSQL = create.select().from("author").getSQL();


How can I create the SQL statement for insert

try {
Class.forName("com.mysql.cj.jdbc.Driver");
dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library?autoReconnect=true&useSSL=false","root","abcd");
DSLContext create = DSL.using(dbConnection,SQLDialect.MYSQL);
String SQl = create.insertInto(Table<?> table("author"), Field<Object> field("id"),Field<Object> field("first_name").Field<Object> field("last_name")).values("1", "askdfj", "kdjvk").getSQL();

}

Answer

You need to use:

create
    .insertInto(table("author"), field("id"), field("first_name"), field("last_name"))
    .values("1", "askdfj", "kdjvk")

Where table and field are a static import of org.jooq.impl.DSL.table and org.jooq.impl.DSL.field.

Note that your code had several syntax errors (eg Table<?> and Field<Object> where they don't belong, and a . instead of a ,).