Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to convert MySQL DECIMAL(39,0) IP address to String

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.

like image 829
Justin Avatar asked Jan 29 '26 14:01

Justin


1 Answers

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.

like image 60
Vineet Reynolds Avatar answered Jan 31 '26 04:01

Vineet Reynolds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!