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中实现这个用例
答案 0 :(得分:4)
在许多JDBC驱动程序中,您不能使用PreparedStatement.setObject(index, null)
,因为JDBC驱动程序需要知道它应该通过线路连接到服务器的{{1>} 类型。我不知道crate.io在这里期待什么(它不是jOOQ中官方支持的数据库),但通常的选择是:
NULL
对于// Using String
stmt.setString(index, null);
// Use setNull()
stmt.setNull(index, Types.OTHER);
方法,很可能是特定于供应商的setNull()
值,比Types
更具体。