문제

How can I get an inet datatype from a postgres database using Java? I have tried:

ResultSet foo = // query goes here //
foo.getString("loopback");

But it throws an SQL exception, "Invalid Column Name," when I do. AFAIK there is no datatype that corresponds to postgres inet in Java. What can I do?

도움이 되었습니까?

해결책

Posted in response to Chris Travers

My original query:

    //Query returning the set of devices in a given market
    String stm ="SELECT device "
                "FROM mopdb.devices d " +
                "INNER JOIN mopdb.markets m " +
                "ON (d.market_id = m.market_id) " +
                "WHERE (m.short = ?) " +
                "ORDER BY d.device_id ";

A "device" in the device table consists of several columns, including loopback and oob, which are (obviously) IP addresses (IPv4, to be clear).

This is the line in which I take the ResultSet and construct a new "device" object

    ResultSet d = DBUtilities.getDevicesFromMarket(m.toString());
    while(d.next()) {
        String loopback, oob;
        Device deviceName = new Device(d.getString("device"), 
                (DBInet.valueOf(d.getString("loopback"))),
                (DBInet.valueOf(oob = d.getString("oob"))), 
                d.getString("traffic_type"), d.getString("clli"),
                d.getString("rack"), d.getInt("market_id"),
                d.getString("codebase"));
        model.addElement(deviceName);
    }

DBInet.valueOf is a static method that turns a String representation of a postgreSQL inet datatype into a DBInet object; a wrapper class for java.net.InetAddress.

The problem was that I was only selecting the "device" column from the device table. Below is the query which works as intended.

    //Query returning the set of devices in a given market
    String stm ="SELECT * " +
                "FROM mopdb.devices d " +
                "INNER JOIN mopdb.markets m " +
                "ON (d.market_id = m.market_id) " +
                "WHERE (m.short = ?) " +
                "ORDER BY d.device_id ";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top