Using JSTL SQL how to get value of PostgreSQL's type INET?

H

Hugh Mandeville

Hi,
From a JSP page (Tomcat 5.0.28), when I try to get the values of an
INET column, it returns empty string. If I do the same select from
psql, it shows the values.

Is there something I have to do in JSP to get the inet value?

I have a simple table in PostgreSQL 7.4.7 that contains a column named
"ip_address" of type "INET".

PostgreSQL Table

Column | Typ | Modifiers

-------------+----------+-----------------------------------------------------
id | integer | not null default
nextval('public.host_id_seq'::text)
ip_address | inet |


Results of Select in pgsql

# select id, ip_address from host;
id | ip_address
----+------------
1 | 127.0.0.1
2 | 127.0.0.1
(2 rows)


JSP Script

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<title>Test JSTL, SQL and PostgreSQL's INET type.</title>
</head>
<body>
<sql:query var="rs" dataSource="jdbc/postgres">
SELECT id, ip_address
FROM host
</sql:query>

Row Count = ${rs.rowCount}<p>

<c:forEach var="row" items="${rs.rows}">
id = ${row.id}, ip_address = ${row.ip_address}<br>
</c:forEach>
</body>
</html>


JSP Output

Row Count = 2
id = 1, ip_address =
id = 2, ip_address =

I have no problem getting columns of other types, like character,
integer, or timestamp. It is only with inet.
Thanks -Hugh
 
S

shakah

Hugh said:
Hi,
From a JSP page (Tomcat 5.0.28), when I try to get the values of an
INET column, it returns empty string. If I do the same select from
psql, it shows the values.

Is there something I have to do in JSP to get the inet value?

[...(good) example snipped...]

Can you get what you want if you cast the ip_address in the JSP query,
e.g.:

<sql:query var="rs" dataSource="jdbc/postgres">
SELECT id, CAST(ip_address AS varchar)
FROM host
</sql:query>
 
H

Hugh Mandeville

Thanks for the help. Casting inet to varchar returns the IP address
with a trailing '/32' (space) '127.0.0.1/32', so I added trim as well.

<sql:query var="rs" dataSource="jdbc/postgres">
SELECT id, TRIM(TRAILING '/32' FROM CAST (ip_address as varchar))
AS ip_address
FROM host
</sql:query>
 
S

shakah

Hugh said:
Thanks for the help. Casting inet to varchar returns the IP address
with a trailing '/32' (space) '127.0.0.1/32', so I added trim as well.

<sql:query var="rs" dataSource="jdbc/postgres">
SELECT id, TRIM(TRAILING '/32' FROM CAST (ip_address as varchar))
AS ip_address
FROM host
</sql:query>

Going a bit off-topic here, but I'm currently evaluating Postgres
(8.0.3) on 64-bit Linux. Do you have any information/opinions/warnings
re using Postgres?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top