如何使用Java从postgres数据库中获取inet数据类型?我试过了:
ResultSet foo = // query goes here //
foo.getString("loopback");
但是当我这样做时,它会抛出一个SQL异常,“无效的列名”。 AFAIK没有与Java中的postgres inet对应的数据类型。我该怎么办?
答案 0 :(得分:2)
发表于对Chris Travers的回应
我的原始查询:
//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 ";
设备表中的“设备”由多个列组成,包括环回和oob,它们(显然)是IP地址(IPv4,要清楚)。
这是我使用ResultSet并构造一个新的“device”对象
的行 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是一个静态方法,它将postgreSQL inet数据类型的String表示形式转换为DBInet对象; java.net.InetAddress的包装类。
问题是我只是从设备表中选择“设备”列。以下是按预期工作的查询。
//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 ";