How to Parse the database Connection String for IPV4 and IPV6

2k Views Asked by At

Im trying to parse the bunch of url.I tried using the URI class in java but it is not perfect for the different set of strings. And also i have to do the parsing on IPV6.The input strings is given below.

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/database?user=userName&password=pass
jdbc:postgresql://host:port/database?charSet=LATIN1&compatible=7.2

jdbc:postgresql://[::1]:5740/accounting (IPV6)

I want the hostname,port,and database name.

Guide me !

1

There are 1 best solutions below

0
On
    String cleanURI = s.substring(5);
    URI uri = URI.create(cleanURI);
    System.out.println(uri.getScheme());
    System.out.println(uri.getHost());
    System.out.println(uri.getPort());
    System.out.println(uri.getPath());