Databases Reference
In-Depth Information
Using the JDBC Driver Manager
One way to connect to a database is through the JDBC Driver Manager by using
the
DriverManager.getConnection
method. This method uses a string con-
taining a connection URL. The following code fragment shows an example of
using the JDBC Driver Manager to connect to Microsoft SQL Server:
Connection conn = DriverManager.getConnection
("jdbc:datadirect:sqlserver://server1:1433;User=test;
Password=secret");
R
EGISTERING THE
JDBC D
RIVER
Registering the DataDirect JDBC drivers with
the JDBC Driver Manager allows the JDBC Driver Manager to load them. To
register a JDBC driver with the JDBC Driver Manager, you must specify the
name of the driver. Note that if you are using Java SE 6, you do not need to regis-
ter the drivers. Java SE 6 automatically registers the drivers with the JDBC
Driver Manager.
You can register the DataDirect JDBC drivers with the JDBC Driver Manager
using any of the following methods:
•
Method 1
—Set the Java system property
jdbc.drivers
using the Java -D
option. The
jdbc.drivers
property is defined as a colon-separated list of
driver class names. For example:
java -Djdbc.drivers=com.ddtek.jdbc.db2.DB2Driver:
com.ddtek.jdbc.sqlserver.SQLServerDriver
registers the DataDirect JDBC DB2 driver and the DataDirect JDBC
Microsoft SQL Server driver.
•
Method 2
—Set the Java property
jdbc.drivers
from within your Java
application or applet. To do this, include the following code fragment in
your
Java
application
or
applet,
and
call
DriverManager.
getConnection
:
Properties p = System.getProperties();
p.put ("jdbc.drivers",
"com.ddtek.jdbc.sqlserver.SQLServerDriver");
System.setProperties (p);
•
Method 3
—Explicitly load the driver class using the standard
Class.
forName
method. To do this, include the following code fragment in your
application or applet and call
DriverManager.getConnection
:
Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver");