【问题标题】:not able to use setobject for custom binding in jooq无法在 jooq 中使用 setobject 进行自定义绑定
【发布时间】:2018-01-17 22:07:15
【问题描述】:
public class DestinationCustomBinding implements Binding<Object, Destination>{


    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final Converter<Object, Destination> converter = new DestinationConverter();

    public Converter<Object, Destination> converter() {
        // TODO Auto-generated method stub

        return converter;
    }

    public void sql(BindingSQLContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
        Param<Integer> param = DSL.val(ctx.convert(converter).value(),Integer.class );

        ctx.render().visit(DSL.val(ctx.convert(converter).value(),Integer.class ));
    }

    public void register(BindingRegisterContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
        ctx.statement().registerOutParameter(ctx.index(), Types.JAVA_OBJECT);
    }

    public void set(BindingSetStatementContext<Destination> ctx) throws SQLException {


        ctx.statement().setObject(ctx.index(), ctx.convert(converter).value(), null);
     //      ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter).value(), null));
    }

    public void set(BindingSetSQLOutputContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
        throw new SQLFeatureNotSupportedException();
    }

    public void get(BindingGetResultSetContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
         ctx.convert(converter).value(ctx.resultSet().getObject(ctx.index()));
    }

    public void get(BindingGetStatementContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
        ctx.convert(converter).value(ctx.statement().getObject(ctx.index()));
    }

    public void get(BindingGetSQLInputContext<Destination> ctx) throws SQLException {
        // TODO Auto-generated method stub
        throw new SQLFeatureNotSupportedException();
    }

}

我想在 public void set(BindingSetStatementContext ctx) 中使用 setObject 而不是 setString,但出现以下错误

Caused by: java.sql.SQLFeatureNotSupportedException: setObject not implemented
    at java.sql.PreparedStatement.setObject(PreparedStatement.java:1291)
    at org.jooq.tools.jdbc.DefaultPreparedStatement.setObject(DefaultPreparedStatement.java:371)
    at com.shn.analytics.db.connection.utils.DestinationCustomBinding.set(DestinationCustomBinding.java:53)
    at org.jooq.impl.DefaultBindContext.bindValue0(DefaultBindContext.java:62)
    at org.jooq.impl.AbstractBindContext.bindValue(AbstractBindContext.java:127)
    ... 11 more

用例:我正在使用 crate db,它接受不带引号的 Json(不是 Json)对象 例如:

create table test_table ( 
  Id Integer, 
  name STRING, 
  test_Object OBJECT
);

insert into test_table(Id, test_Object) 
values (10, 'test_Name', {city = 'random_city'});

如何在jooq中实现这个用例

【问题讨论】:

  • 你使用的是什么数据库/JDBC驱动?
  • 我正在使用 crate db (crate.io)。 jdbc 驱动程序是 (crate.io/docs/clients/jdbc) postgres,我正在使用 HikariCP 数据源来获取连接
  • `DestinationConverter`的定义是什么?

标签: java sql jooq


【解决方案1】:

在许多 JDBC 驱动程序中,您不能使用 PreparedStatement.setObject(index, null),因为 JDBC 驱动程序需要知道 NULL 的什么类型,它应该通过连线连接到服务器。我不知道 crate.io 在这里期望什么(它不是 jOOQ 中官方支持的数据库),但通常的选项是:

// Using String
stmt.setString(index, null);

// Use setNull()
stmt.setNull(index, Types.OTHER);

在 setNull() 方法的情况下,很可能存在特定于供应商的 Types 值,比 Types.OTHER 更具体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 2021-09-08
    • 2021-06-05
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多