1

PostgreSQL has the nifty inet type, but there's no corresponding JDBC type for it. How do I store an IP address in an inet column using Java?

I'm using MyBatis, not raw JDBC, but the problem is with JDBC itself.

1 Answer 1

2

This question has been asked and answered several times on the postgresql mailing list (2001, 2015) as well as several other places.

Since there's no inet in JDBC, the easiest solution is pass the IP address in String form, then cast to inet in the query itself.

In mybatis this looks like this:

public interface MyMapper {

    @Insert("INSERT INTO my_table (ip) VALUES (#{ip}::INET)")
    int insert(@Param("ip") String ip);

}

Another solution is to wrap the address in a custom class, that implements java.sql.SQLData, then simply pass your own class as an Object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.