0
votes

I'm trying to insert an ipaddress into a Postgres Table from a Java project.

String ipIns = "INSERT INTO trusted_domains(id, from_ip, to_ip) VALUES (?, ?, ?);";
pstmt = conn.prepareStatement(ipIns);
pstmt.setInt(1, Integer.parseInt(id));
pstmt.setObject(2, InetAddress.getByName(fromIP));
pstmt.setObject(3, InetAddress.getByName(toIP));
pstmt.execute();

I get an exception for this statement pstmt.setObject(2, InetAddress.getByName(fromIP));

org.postgresql.util.PSQLException: ERROR: invalid input syntax for type inet:

Kindly help me on this exception

Thanks --SD

1
I got it, its pstmt.setObject(2, fromIP, Types.OTHERS); Thanks --SD - SDS

1 Answers

1
votes

You have found it by now, but for the general public:

The inet data type accepts IPv4 and IPv6 hosts and networks, not domain names.
Works:

SELECT '192.168.1.251'::inet
SELECT '123.45.67.89/32'::inet
SELECT '::ffff:10.4.3.2'::inet
SELECT '1234:ef8:345:ad:934:efff:aaa:d1f1/128'::inet

Doesn't work:

SELECT 'stackoverflow.com'::inet