2
package com.jooq.demo;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.jooq.utils.DBUtils;

import static org.jooq.impl.DSL.*;
import org.jooq.*;
import org.jooq.impl.*;


public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection conn = null;
        conn=DBUtils.getConnection();
        DSLContext create = DSL.using(conn, SQLDialect.MYSQL);

        Query query = create.select(val(1),val(2),val(3),val(4),val(5),val(6),val(7),val(8),val(9),val(10),val(11),val(12))
                            .from(table("T"));
        query.bind(1,"C1");
        query.bind(2,"C2");
        List<Object> val = query.getBindValues();
        System.out.println(val.get(0));

    }

}
Output:-
null

I am using JAVA 8,with mysql connector version 5.1.8, JOOQ version 3.14.11

It is printing null in the Output. I was expecting binding to take place and print C1 in the output.

What's the mistake in my code?

pranay
  • 35
  • 3
  • What are you actually trying to do, i.e. what actual problem does this example stand for? – Lukas Eder Jun 04 '21 at 19:40
  • I want those field in select() to take values which are stored in an array. Like select(?,?,?.and so on) and question marks would be replaced by values of the array in the query string – pranay Jun 04 '21 at 20:08
  • But why not delay the creation of the entire select to when the array is known? You'll get a much more readable / maintainable result. If you're interested, you could ask a new question about "how do I best create a dynamic query given ...", and I'll be very happy to show you an example of what I mean. – Lukas Eder Jun 05 '21 at 07:58
  • okk, thanks for ur help.I have created a new ques [link](https://stackoverflow.com/questions/67861876/generate-dynamic-query-using-jooq) – pranay Jun 07 '21 at 05:08
  • I'm glad I asked. So your *actual* question isn't about [bind values](https://www.jooq.org/doc/latest/manual/sql-building/bind-values/) at all, but about column references. – Lukas Eder Jun 07 '21 at 07:01

1 Answers1

1

The reason for this behaviour is that val(1), val(2), etc. are of type SQLDataType.INTEGER, and your value "C1" cannot be converted to that type. Instead of throwing an exception, for historic reasons, the conversion fails silently and puts a null bind value there, instead.

See: https://github.com/jOOQ/jOOQ/issues/3377

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509