Search code examples
javasqlbindingnulljooq

Indexed Binding printing NULL in JOOQ


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?


Solution

  • 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