[ Back | Previous | Next ]

What does a JDBC URL look like when I want to connect to a remote database?

Package:
java.sql.*
Product:
JDK
Release:
1.1.x
Related Links:
General
General
General
General
CallableStatement
Comment:
2.1.3     JDBC URLs 

A JDBC URL provides a way of identifying a database so that the appropriate driver will
recognize it and establish a connection with it. Driver writers are the ones who actually determine
what the JDBC URL that identifies their particular driver will be. Users do not need to worry
about how to form a JDBC URL; they simply use the URL supplied with the drivers they are
using. JDBC's role is to recommend some conventions for driver writers to follow in structuring
their JDBC URLs. 

Since JDBC URLs are used with various kinds of drivers, the conventions are of necessity very
flexible. First, they allow different drivers to use different schemes for naming databases. The odbc
subprotocol, for example, lets the URL contain attribute values (but does not require them). 

Second, JDBC URLs allow driver writers to encode all necessary connection information within
them. This makes it possible, for example, for an applet that wants to talk to a given database to
open the database connection without requiring the user to do any system administration chores. 

Third, JDBC URLs allow a level of indirection. This means that the JDBC URL may refer to a
logical host or database name that is dynamically translated to the actual name by a network
naming system. This allows system administrators to avoid specifying particular hosts as part of the
JDBC name. There are a number of different network name services (such as DNS, NIS, and
DCE), and there is no restriction about which ones can be used. 

The standard syntax for JDBC URLs is shown below. It has three parts, which are separated
by colons: 

      jdbc::

The three parts of a JDBC URL are broken down as follows: 

   1.jdbc-the protocol. The protocol in a JDBC URL is always jdbc. 

          

   2.-the name of the driver or the name of a database connectivity mechanism,
      which may be supported by one or more drivers. A prominent example of a subprotocol
      name is "odbc", which has been reserved for URLs that specify ODBC-style data source
      names. For example, to access a database through a JDBC-ODBC bridge, one might use
      a URL such as the following: 

            jdbc:odbc:fred

       In this example, the subprotocol is "odbc", and the subname "fred" is a local 
      ODBC data source.


            If one wants to use a network name service (so that the database name in the
            JDBC URL does not have to be its actual name), the naming service can be
            the subprotocol. So, for example, one might have a URL like: 

            jdbc:dcenaming:accounts-payable

       In this example, the URL specifies that the local DCE naming service should 
      resolve the database name "accounts-payable" into a more specific name that 
      can be used to connect to the real database.


   3.-a way to identify the database. The subname can vary, depending on the
      subprotocol, and it can have a subsubname with any internal syntax the driver writer
      chooses. The point of a subname is to give enough information to locate the database. In
      the previous example, "fred" is enough because ODBC provides the remainder of the
      information. A database on a remote server requires more information, however. If the
      database is to be accessed over the Internet, for example, the network address should be
      included in the JDBC URL as part of the subname and should follow the standard URL
      naming convention of 

       //hostname:port/subsubname 

 Supposing that "dbnet" is a protocol for connecting to a host on the Internet, a 
JDBC URL might look like this:

      jdbc:dbnet://wombat:356/fred

2.1.4     The "odbc" Subprotocol

The subprotocol odbc is a special case. It has been reserved for URLs that specify ODBC-style
data source names and has the special feature of allowing any number of attribute values to be
specified after the subname (the data source name). The full syntax for the odbc subprotocol is: 

      jdbc:odbc:[;=]*

Thus all of the following are valid jdbc:odbc names: 

      jdbc:odbc:qeor7
      jdbc:odbc:wombat
      jdbc:odbc:wombat;CacheSize=20;ExtensionCase=LOWER
      jdbc:odbc:qeora;UID=kgh;PWD=fooey

1