JDBC URLs ...not really URLs?

A

Adam Monsen

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.
And neither jdbc nor mysql are valid schemes... what's going on here?

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). Anyone know
a more robust way?

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).
 
L

Lew

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:postgresql://localhost/sample http://java.sun.com

Illegal URL path: "jdbc:postgresql://localhost/sample"

input = "jdbc:postgresql://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" }
 
T

Tom Anderson

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.
And neither jdbc nor mysql are valid schemes... what's going on here?

They aren't really URLs. They're just a bit like URLs.

Mostly. The format is specific to the driver, and not all of them use a
URL-like format. See:

http://www.petefreitag.com/articles/jdbc_urls/

For instance, a valid postgresql URL can look like:

jdbc:postgresql:MYDATABASE
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). Anyone know a
more robust way?

Maybe opening a connection to the database referenced by the URL, and then
reading metadata from the connection object. Obviously, far less practical
and efficient than parsing the URL.

tom
 
L

Lew

They aren't really URLs. They're just a bit like URLs.

/Au contraire/, they are really URLs, making them exactly like URLs.
Mostly. The format is specific to the driver, and not all of them use a
URL-like format. See:
<http://www.petefreitag.com/articles/jdbc_urls/>

Every single example on that link shows URLs for the URLs, and even
calls them "URLs". Same for
<http://www.redmountainsw.com/wordpress/archives/jdbc-connection-urls>

They are URLs because they are URIs that locate the resource, in this
case the database, and they are formed according the RFC for URIs.
<http://www.ietf.org/rfc/rfc2396.txt>
 
M

Michael Rauscher

Adam said:
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.
And neither jdbc nor mysql are valid schemes... what's going on here?

According to RFC-1738, section 5, a generic URL has the format

genericurl = scheme ":" schemepart

with

scheme = 1*[ lowalpha | digit | "+" | "-" | "." ]
schemepart = *xchar | ip-schemepart

A schemepart is made up of characters (including reserved characters) or
an ip-schemepart which is "//" login [ "/" urlpath ].

A jdbc url has the following format

jdbc:subprotocol:subname

"jdbc" is the scheme and "mysql://localhost:3305/mifos?useUnicode=true"
is the schemepart. All characters herein are legal.

RFT-1738, section 2.2 states

The characters ";",
"/", "?", ":", "@", "=" and "&" are the characters which may be
reserved for special meaning within a scheme.

So, the "jdbc" scheme uses ":" to divide the schemepart into two further
parts: subprotocol and subname.

Bye
Michael
 
O

Owen Jacobson

/Au contraire/, they are really URLs, making them exactly like URLs.

I was going to argue that they are URIs but not URLs (based on the lack
of a strict scheme://host/bits structure). Then I thought about
<and <mailto:...> URLs, which are URLs (as well as URIs), and
realized that my brain is full of lies on this matter. Thanks, Lew.

-o
 
M

Michael Rauscher

Lew said:
OK. An absolute URI is written as

<scheme>:<scheme-specific-part>

The interpretation of the <scheme-specific-part> depends on the scheme.

For "jdbc:mysql://localhost:3305/mifos?useUnicode=true":

* "jdbc" is the scheme
* "mysql://localhost:3305/mifos?useUnicode=true" is the
<scheme-specific-part> which is an opaque_part according to appendix A.

Bye
Michael
 
O

Owen Jacobson

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.
And neither jdbc nor mysql are valid schemes... what's going on here?

jdbc is a valid URI-scheme, but not a registered one. Sun effectively
defines the format for it, and it's unlikely that you'll find
conflicting uses in the wild.

The only format Sun imposes, though, is that jdbc URIs have the format

jdbc:<driver-identifier>:<driver-part>

the driver-identifier is fairly constrained, but the driver-part is
almost entirely up to the first loaded JDBC driver that accepts the
driver-identifier.
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). Anyone know
a more robust way?

Ask JDBC to connect to it. There is no general-purpose parser for JDBC
URIs, because there is no canonical format for the driver-part. MySQL
uses //host:port/database?params, similar to HTTP, but other drivers
use other formats (PostgreSQL's has already been mentioned, but
Oracle's is also notable for being way too flexible for its own good).

If you have a short list of drivers you care about and don't mind
falling back to the unparsed URL, you can probably either call those
drivers' parsers or write a compatible parser. JDBC drivers tend to
have their connection URL formats documented fairly well.

The URI and URL classes in java.net are oriented towards the common
network URI syntaxes (ftp, http, and so on), all of which do have
scheme://host/path structures, and are inappropriate here.

-o
 
A

Arne Vajhøj

Michael said:
Adam said:
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.
And neither jdbc nor mysql are valid schemes... what's going on here?

According to RFC-1738, section 5, a generic URL has the format

genericurl = scheme ":" schemepart

with

scheme = 1*[ lowalpha | digit | "+" | "-" | "." ]
schemepart = *xchar | ip-schemepart

Which means that almost anything with a colon is a valid URI.

person:Adam
person:Michael
person:Arne

:)

Arne
 
D

Donkey Hottie

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.
And neither jdbc nor mysql are valid schemes... what's going on here?

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). Anyone know
a more robust way?

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).

Java URL supports only URLs (schemes) that has an URLConnection
implementation in Java library.

For example a valid URL ldap://localhost:389 is not supported by Java URL.

URL can be URL, while not supported by Java URL class...
 
A

Adam Monsen

* "jdbc" is the scheme
* "mysql://localhost:3305/mifos?useUnicode=true" is the
<scheme-specific-part> which is an opaque_part according to appendix A.

Folks,

Thank you for help interpreting the URI/URL RFCs as well as the Java
documentation! Especially the part of the URI RFC (#2396) I completely
missed discussing opaque URIs.

As for connecting then using DatabaseMetaData to get information, I
recall not being able to retrieve the hostname and port via that
class.

While this

String mysqlUrl = "jdbc:mysql://localhost:3305/mifos?
useUnicode=true";
URI uri = new URI(new URI(mysqlUrl).getSchemeSpecificPart());
// uri.getPath()
// uri.getPort()
// uri.getHost()
// uri.getQuery()
// etc.

works with some MySQL database URLs I've tested, sounds like more work
would be required to support parsing URLs for connections to other
types of databases. For the purposes of Mifos, simply printing out the
JDBC connection URL should be sufficient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top