java postgresql 数据类型,在postgresql中以范围类型持久化Java对象

I have a Postgres table with several columns of type numrange and int4range.

I want to persist data in it from Java. Currently I have this data in a Java class like this:

class Range {

private Integer minimum;

private Integer maximum;

// more code...

}

I'm using the JDBC Driver and the java.sql.*and I've tried several things, without success:

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

This gives me the following error:

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.

I cannot specify the type, since it does not exist in the java.sql.Types class.

解决方案

It can be done by using the Types.OTHER.

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

It was also required to include a toString() method in my Range class:

class Range {

// more code...

public String toString() {

return String.format("[%d, %d]", minimum, maximum);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EMQ X Broker 支持多种持久化方式,包括 PostgreSQL 持久化。通过将数据写入 PostgreSQL 数据库,可以保证数据的可靠性和持久性,并且可以方便地进行数据的查询和分析。 要使用 PostgreSQL 持久化,需要进行以下步骤: 1. 安装 PostgreSQL 数据库,并创建一个数据库用于存储 EMQ X Broker 的数据。 2. 在 EMQ X Broker 的配置文件中设置 PostgreSQL 数据库的连接信息和相关参数,例如: ``` ## PostgreSQL ##-------------------------------------------------------------------- ## PostgreSQL database ##-------------------------------------------------------------------- ## The PostgreSQL database used by the EMQ X Broker for persistence. ## The default database is "emqx". ## ## Note: The specified user must be authorized to create tables and indexes ## in the database. ## ## Example: ## emqx.persistence.pgsql.server = postgresql://user:password@localhost:5432/emqx ## ## emqx.persistence.pgsql.server = postgresql://user:password@localhost:5432/emqx emqx.persistence.pgsql.server = postgresql://postgres:password@localhost:5432/emqx ``` 3. 启动 EMQ X Broker,数据将被写入 PostgreSQL 数据库中。 4. 可以使用 SQL 查询语句来查询和分析数据。 例如,以下 SQL 查询语句可以查询指定时间范围内的消息数量: ``` SELECT COUNT(*) FROM emqx_messages WHERE timestamp >= '2021-01-01 00:00:00' AND timestamp < '2022-01-01 00:00:00'; ``` 通过使用 PostgreSQL 持久化,可以实现 EMQ X Broker 数据的可靠存储和高效查询,为企业提供更加稳定和可靠的消息服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值