How to parse a JDBC url to get hostname,port etc?

java, jdbc, parsing, url

Solution

Start with something like this:

String url = "jdbc:derby://localhost:1527/netld;collation=TERRITORY_BASED:PRIMARY";
String cleanURI = url.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());

Output from the above:

derby
localhost
1527
/netld;collation=TERRITORY_BASED:PRIMARY

Problem

How can I parse a JDBC URL (oracle or sqlserver) to get the hostname, port, and database name. The formats of the URL are different.

Original source

Related problems