To connect to a database you can use a "URL", yes? Like this, for
instance...
jdbc:mysql://localhost:3305/mifos?useUnicode=true
But ":" is not a valid character for the scheme component of a URL.
java.net.URL is geared to accessing files.
And neither jdbc nor mysql are valid schemes... what's going on here?
Completely false. "jdbc" is a valid scheme. "mysql" is the
subprotocol for that scheme.
I find to actually parse one of these, I have to first remove "jdbc:",
then I can parse it with java.net.URI (not java.net.URL).
I'm guessing that java.net.URI then calls "mysql" the scheme, which is
wrong. The scheme is "jdbc".
Anyone know a more robust way?
Yes, don't remove the "jdbc:".
And, by the way, I want to parse a JDBC "URL" so I can display
connection hostname, port, etc. in a Web application (this one:
http://mifos.org).
The "jdbc" scheme uses an "opaque URI", not a "hierarchical URI".
There is some information relevant to your question in the
java.net.URI Javadocs:
<
http://java.sun.com/javase/6/docs/api/java/net/URI.html>
See "opaque URI". Also look up "opaque URI" in the RFC:
<
http://www.ietf.org/rfc/rfc2396.txt>
<sscce class="eegee.Urika">
package eegee;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class Urika
{
private URI uri;
private URL url;
public Urika( String path )
{
setPath( path );
}
public final URI getUri()
{
return uri;
}
public final URL getUrl()
{
return url;
}
public final String toString()
{
StringBuilder sb = new StringBuilder("URI: ");
if ( this.uri == null )
{
sb.append( "null" );
}
else
{
sb.append( "{ scheme = \""+ this.uri.getScheme() +"\",
" );
sb.append( "specific = \""+ this.uri.getSchemeSpecificPart
()
+"\" } " );
}
sb.append( "\nURL: " );
if ( this.url == null )
{
sb.append( "null " );
}
else
{
try
{
URI ri = this.url.toURI();
sb.append( "{ scheme = \""+ ri.getScheme() +"\", " );
sb.append( "specific = \""+ ri.getSchemeSpecificPart()
+"\" } " );
}
catch ( URISyntaxException exc )
{
System.err.println( "Illegal URI path: \""+ this.url
+"\"\n" );
}
}
return sb.toString();
}
public final void setPath( String path )
{
if ( path == null )
{
this.uri = null;
this.url = null;
return;
}
try
{
this.uri = new URI( path );
}
catch ( URISyntaxException exc )
{
System.err.println( "Illegal URI path: \""+ path
+"\"\n" );
}
try
{
this.url = new URL( path );
}
catch ( MalformedURLException exc )
{
System.err.println( "Illegal URL path: \""+ path
+"\"\n" );
}
}
public static void main( String [] args )
{
for ( String path : args )
{
Urika urika = new Urika( path );
System.out.println( "input = \"" + path + "\"" );
System.out.println( urika.toString() );
System.out.println();
}
}
}
</sscce>
$ javac -d build/classes/ src/eegee/Urika.java
$ java -cp build/classes/ eegee.Urika \
jdbc

ostgresql://localhost/sample
http://java.sun.com
Illegal URL path: "jdbc

ostgresql://localhost/sample"
input = "jdbc

ostgresql://localhost/sample"
URI: { scheme = "jdbc", specific = "postgresql://localhost/sample" }
URL: null
input = "
http://java.sun.com"
URI: { scheme = "http", specific = "//java.sun.com" }
URL: { scheme = "http", specific = "//java.sun.com" }