Java InetAddress转换为PostgreSQL inet,反之亦然

时间:2019-05-31 16:35:17

标签: java postgresql ip

我想使用PSQL inet数据类型,但它不接受字符串(getRemoteAddr())或byte [](InetAddress)。

有没有一种方法可以使用Java正确转换?

2 个答案:

答案 0 :(得分:1)

只需调用InetAddress.getByName(字符串主机)并传入您的文本IP地址即可。

从JavaDoc:主机名可以是计算机名称,例如“ java.sun.com”,也可以是其IP地址的文本表示形式。

〜InetAddress javadoc

如果您要询问如何从IP中获取字符串:

String getHostAddress()           以文本形式返回IP地址字符串。

String getHostName()           获取此IP地址的主机名。



对不起,我没有更多帮助。

答案 1 :(得分:0)

遇到同样的问题。我能够使用 InetAddress 读取 Inet Postgres 类型,但在将相同的 InetAddress 类型插入具有 inet 类型的 postgres 时遇到错误。

通过定义自定义休眠类型解决了

PgInet->

    public class PgInet implements Serializable {

    private InetAddress address;

    public PgInet() {}

    public PgInet(InetAddress address) {
        this.address = address;
    }

    public InetAddress getAddress() {
        return address;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((address == null) ? 0 : address.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof PgInet)) {
            return false;
        }
        PgInet other = (PgInet) obj;
        if (address == null) {
            if (other.address != null) {
                return false;
            }
        } else if (!address.equals(other.address)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("PgInet [address=");
        builder.append(address);
        builder.append("]");
        return builder.toString();
    }

}

PGInetType ->

    public class PgInetType implements UserType {

    public PgInetType() {}

    @Override
    public Object assemble(Serializable cached, Object owner) {
        return deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) {
        if (value != null) {
            return new PgInet(((PgInet) value).getAddress());
        }
        return null;
    }

    @Override
    public Serializable disassemble(Object value) {
        return (value != null) ? (Serializable) deepCopy(value) : null;
    }

    @Override
    public boolean equals(Object x, Object y) {
        return x == y || ( x != null && y != null && x.equals( y ) );
    }

    @Override
    public int hashCode(Object x) {
        return (x != null) ? x.hashCode() : 0;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names,
                              SharedSessionContractImplementor session, Object owner) throws SQLException {
        PgInet address = null;

        String ipStr = rs.getString(names[0]);
        if (ipStr != null) {
            try {
                address = new PgInet(InetAddress.getByName(ipStr));
            } catch (UnknownHostException e) {
                throw new HibernateException(e);
            }
        }

        return address;
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index,
                            SharedSessionContractImplementor session) throws SQLException {
        if (value == null) {
            st.setNull(index, Types.VARCHAR);
        }
        else {
            PGobject pgObj = new PGobject();
            pgObj.setType("inet");
            pgObj.setValue(((PgInet) value).getAddress().getHostAddress());
            st.setObject(index, pgObj);
        }
    }

    @Override
    public Object replace(Object original, Object target, Object owner) {
        return deepCopy(original);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Class returnedClass() {
        return PgInet.class;
    }

    @Override
    public int[] sqlTypes() {
        return new int[] {Types.OTHER};
    }

}

用法->

@TypeDefs({
        @TypeDef(name="pgInet", typeClass=PgInetType.class)

})
public class Test{
    @Column(name = "ip")
    @Type(type="pgInet")
    private PgInet ip;
}