gpt4 book ai didi

java - 以范围类型将 Java 对象保存在 postgresql 中

转载 作者:行者123 更新时间:2023-11-29 13:00:24 32 4
gpt4 key购买 nike

我有一个 Postgres 表,其中有几列类型为 numrangeint4range

我想从 Java 中持久化数据。目前我在这样的 Java 类中有这些数据:

class Range<T> {
private Integer minimum;
private Integer maximum;
// more code...
}

我正在使用 JDBC 驱动程序和 java.sql.*,我已经尝试了几种方法,但都没有成功:

pstmt.setObject(7, myObject.price()); // price() returns a Range object

这给了我以下错误:

Can't infer the SQL type to use for an instance of com.scmspain.admatching.domain.Range. Use setObject() with an explicit Types value to specify the type to use.

我无法指定类型,因为它不存在于 java.sql.Types 类中。

最佳答案

这可以通过使用 Types.OTHER 来完成。

pstmt.setObject(7, myObject.price(), Types.OTHER);

还需要在我的 Range 类中包含一个 toString() 方法:

class Range<T> {
// more code...

public String toString() {
return String.format("[%d, %d]", minimum, maximum);
}
}

关于java - 以范围类型将 Java 对象保存在 postgresql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114801/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com