views:

390

answers:

2

So I'm told that the DBAs here are going to be storing IP addresses in a DECIMAL(39,0) field in our MySQL database.

How can I go about translating that into a String to pass on to our other applications?

Note: Our databases can't take any more load, so as much of the work as possible needs to be done in the Java application.

Note2: IPv6 support is a must.

+1  A: 

Use

INET_NTOA

eg: SELECT INET_NTOA(num) AS ipaddr;

and

INET_ATON

eg: INSERT INTO addrTbl VALUES (..., INET_ATON('192.168.0.1'), ...)

This works because IPv4 was originally designed to have IP addresses defined as 32-bit numbers. The dot-decimal notation is for readbility.

Update

If you wish to perform the transformation from a number to a dot-decimal notation in Java, the MindProd site has an example.

Vineet Reynolds
Thanks. I forgot to mention though... using MySQL function calls or stored procedures is absolutely out of the question. Our database memory is at a premium.
Justin
I've updated the answer, assuming that you intend to do this in Java.
Vineet Reynolds
That is more helpful, but still doesn't really cut it. Do you know of any methods that have IPv6 support?
Justin
That's a bit difficult with primitives (due to 128 bit sizes for IPv6 addresses), but doable.
Vineet Reynolds
A: 

Problem solved, I convinced the database guys to just store it as a VARCHAR instead.

Justin
I was attempting to construct an IPv6 number to address converter. Like I said earlier, it is doable with BigDecimal (that maps to the MySQL DECIMAL type), but it is a toughie.
Vineet Reynolds