By: Reema Sen in JDBC Tutorials on 2007-10-12
Using the DriverManager Class
DriverManager class works with the
interface to manage the set of drivers available to a JDBC client. When the
client requests a connection and provides a URL, the
is responsible for finding a driver that recognizes the URL and for using it to
connect to the corresponding data source. Connection URLs have the following
dbName portion of the URL identifies a specific database. A
database can be in one of many locations: in the current working directory, on
the classpath, in a JAR file, in a specific Java DB database home directory, or
in an absolute location on your file system.
If you are using a vendor-specific driver, such as Oracle, the documentation
will tell you what subprotocol to use, that is, what to put after
in the JDBC URL. For example, if the driver developer has registered the name
as the subprotocol, the first and second parts of the JDBC URL will be
. The driver documentation will also give you guidelines for the rest of the
JDBC URL. This last part of the JDBC URL supplies information for identifying
the data source.
getConnection method establishes a connection:
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
In place of "
myLogin " you insert the name you use to
log in to the DBMS; in place of "
myPassword " you insert
your password for the DBMS. So, if you log in to your DBMS with a login name of
Fernanda " and a password of "
" just these two lines of code will establish a connection:
String url = "jdbc:derby:Fred"; Connection con = DriverManager.getConnection(url, "Fernanda", "J8");
If one of the drivers you loaded recognizes the JDBC URL supplied to the
DriverManager.getConnection, that driver establishes a
connection to the DBMS specified in the JDBC URL. The
class, true to its name, manages all of the details of establishing the
connection for you behind the scenes. Unless you are writing a driver, you
probably won't use any of the methods in the interface
DriverManager method you really need to know is
The connection returned by the method
is an open connection you can use to create JDBC statements that pass your SQL
statements to the DBMS. In the previous example,
con is an open
connection, and you use it in the examples that follow.
Using a DataSource Object for a connectionUsing a
DataSourceobject increases application portability by making it possible for an application to use a logical name for a data source instead of having to supply information specific to a particular driver. The following example shows how to use a
DataSourceto establish a connection:
You can configure a
DataSource using a tool or manually. For
example, Here is an example of a
InitialContext ic = new InitialContext() DataSource ds = ic.lookup("java:comp/env/jdbc/myDB"); Connection con = ds.getConnection(); DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource() ds.setPort(1527); ds.setHost("localhost"); ds.setUser("APP") ds.setPassword("APP"); Connection con = ds.getConnection();
DataSource implementations must provide getter and setter
methods for each property they support. These properties typically are
initialized when the
DataSource object is deployed.
VendorDataSource vds = new VendorDataSource(); vds.setServerName("my_database_server"); String name = vds.getServerName();
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JDBC )
Latest Articles (in JDBC)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews