JDBC URLs ...not really URLs?

Discussion in 'Java' started by Adam Monsen, Feb 6, 2009.

  1. Adam Monsen

    Adam Monsen Guest

    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).
    Adam Monsen, Feb 6, 2009
    #1
    1. Advertising

  2. Adam Monsen

    Lew Guest

    On Feb 6, 12:21 pm, Adam Monsen <> wrote:
    > 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" }

    --
    Lew
    Lew, Feb 6, 2009
    #2
    1. Advertising

  3. Adam Monsen

    Tom Anderson Guest

    On Fri, 6 Feb 2009, Adam Monsen wrote:

    > 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

    --
    Lilith doesn't exist, but it's an interesting story.
    Tom Anderson, Feb 6, 2009
    #3
  4. Adam Monsen

    Lew Guest

    On Feb 6, 2:25 pm, Tom Anderson <> wrote:
    > On Fri, 6 Feb 2009, Adam Monsen wrote:
    > > 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.


    /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>

    --
    Lew
    Lew, Feb 6, 2009
    #4
  5. Adam Monsen wrote:
    > 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
    Michael Rauscher, Feb 7, 2009
    #5
  6. Adam Monsen

    Lew Guest

    Michael Rauscher wrote:
    > According to RFC-1738, section 5, a generic URL has the format


    That RFC has been superseded.
    > This document defines the generic syntax of URI,
    > including both absolute and relative forms, and
    > guidelines for their use; it revises and
    > replaces the generic definitions in RFC 1738 and RFC 1808.

    <http://www.ietf.org/rfc/rfc2396.txt>

    --
    Lew
    Lew, Feb 7, 2009
    #6
  7. On 2009-02-06 14:42:32 -0500, Lew <> said:

    > On Feb 6, 2:25 pm, Tom Anderson <> wrote:
    >> On Fri, 6 Feb 2009, Adam Monsen wrote:
    >>> 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.

    >
    > /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
    <news:...> 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
    Owen Jacobson, Feb 7, 2009
    #7
  8. Lew wrote:
    > Michael Rauscher wrote:
    >> According to RFC-1738, section 5, a generic URL has the format

    >
    > That RFC has been superseded.
    >> This document defines the generic syntax of URI, including both
    >> absolute and relative forms, and guidelines for their use; it revises
    >> and replaces the generic definitions in RFC 1738 and RFC 1808.

    > <http://www.ietf.org/rfc/rfc2396.txt>
    >

    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
    Michael Rauscher, Feb 7, 2009
    #8
  9. On 2009-02-06 12:21:17 -0500, Adam Monsen <> 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?


    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
    Owen Jacobson, Feb 7, 2009
    #9
  10. Adam Monsen

    Arne Vajhøj Guest

    Michael Rauscher wrote:
    > Adam Monsen wrote:
    >> 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
    Arne Vajhøj, Feb 8, 2009
    #10
  11. Adam Monsen <> wrote in news:d60f1415-0c5a-4b59-8868-
    :

    > 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...
    Donkey Hottie, Feb 8, 2009
    #11
  12. Adam Monsen

    Adam Monsen Guest

    > * "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.
    Adam Monsen, Feb 8, 2009
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Wansbrough

    Re: jdbc help:sun.jdbc.odbc.JdbcOdbcDriver

    Keith Wansbrough, Aug 16, 2004, in forum: Java
    Replies:
    0
    Views:
    733
    Keith Wansbrough
    Aug 16, 2004
  2. Betty
    Replies:
    1
    Views:
    9,843
    Juha Laiho
    May 21, 2005
  3. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,016
    robert
    Feb 11, 2006
  4. Bruce
    Replies:
    4
    Views:
    735
    Bjorn Abelli
    Mar 25, 2006
  5. Lew
    Replies:
    0
    Views:
    501
Loading...

Share This Page