0

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?

5
  • 1
    "But it throws an SQL exception when I do." Which one? Commented Mar 21, 2013 at 16:25
  • I feel dumb. I hadn't even checked. It throws, "Invalid Column Name" Commented Mar 21, 2013 at 16:31
  • 1
    Please also paste your query and any referenced table schemas. Commented Mar 21, 2013 at 16:32
  • No need. I figured it out. I hadn't set my Select statement to grab all the columns I needed. Thanks for reminding me to print my exceptions. Commented Mar 21, 2013 at 16:32
  • 1
    Can you add that to the answers section? Commented Mar 22, 2013 at 10:30

1 Answer 1

2

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 ";
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.