JdbcTemplate 和 inet 数据类型
我的一张表的列类型为 inet。当我尝试使用 String 作为 inet 列的类型执行插入操作时,它说“列“ip”的类型为 inet,但表达式的类型为字符变化:”,这是完全有效的异常。 现在,我的问题是如何指示 jdbcTemplate 使用 inet 类型而不是 String。我正在尝试类似的操作:
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("ip",new SqlParameterValue(Types.??, conn.getIPAddress()));
inet 类型未在 Types 类中列出,我应该传递什么?
PS 我使用的是 PostgresSql 版本 8.4.4。
One of my table has the column type as inet. And when I try to perform insert operation using String as the type for inet column, it's saying "column "ip" is of type inet but expression is of type character varying:", which is perfectly valid exception.
Now, my question is how do I instruct jdbcTemplate to use inet type instead of String. I'm trying something like:
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("ip",new SqlParameterValue(Types.??, conn.getIPAddress()));
The inet type is not listed in Types class, what shall I pass?
P.S. I'm using PostgresSql version 8.4.4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能性是在 sql 中的占位符周围添加强制转换操作,例如:
这会将 IP 地址从文本转换为 inet 服务器端,因此任何有关格式错误的地址的错误都必须从服务器解码。或只是在插入之前验证它并希望。
One possibility is to add a cast operation around the placeholder in your sql, e.g.:
This converts the IP address from text to inet server-side instead, so any errors about a badly-formatted address will have to be decoded from the server... or just validate it before inserting and hope.
您应该使用 Typer.OTHER 让 postgresql 尝试推断正确的类型。
您可以将“stringtype=unspecified”添加到 JDBC 连接参数中。这指示驱动程序让 postgres 尝试推断正确的类型。
第三种可能性是子类化 PGobject。在较旧的驱动程序中,提供了这样的子类 PGinet,但显然由于某种原因而放弃了对此的支持。我认为几何类型仍然存在,但网络类型不再包含在驱动程序包中。
You should use Typer.OTHER to let postgresql try to infer the right type.
You can add "stringtype=unspecified" to the JDBC connection parameters. This instructs the driver to let postgres try to infer the right type.
A third possibility is to subclass PGobject. In older driver such a subclass PGinet was provide, but apparently support for this was dropped for one reason or another. The geometric types are still present, but the network types are no longer in the driver package, I think.