JdbcTemplate and inet data type

One of my tables has a column type like inet. And when I try to perform an insert operation using String as the type for the inet column, it says that "column" ip "is of type inet, but the expression has a type character that changes:", which is a perfectly valid exception. Now, my question is how can I give jdbcTemplate instructions to use the 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 specified in the Types class, what should I pass?

PS I am using PostgresSql version 8.4.4.

+7
postgresql jdbctemplate
source share
2 answers

One possibility is to add a cast operation around the placeholder in your sql, for example:

 insert into t(ip) values(?::inet) 

This converts the IP address from text to inet on the server side, so any errors regarding the poorly formatted address must be decoded from the server ... or just confirm it before insertion and hope.

+6
source share

You must use Typer.OTHER for postgresql to try to infer the correct type.

You can add "stringtype = unspecified" to the JDBC connection parameters. This instructs the driver to let postgres try to infer the correct type.

A third possibility is to subclass PGobject. In older drivers, such a subclass of PGinet was provided, but apparently support for this was dropped for one reason or another. Geometric types are still present, but network types are no longer included in the driver package.

+3
source share

All Articles