|
| |
Connecting to a database with JDBC involves the following:
- Obtain a JDBC Driver and Database
There are many vendors who supply JDBC drivers for a
number of different databases systems. JavaSoft keeps
a list of available drivers, from which you
can pick. Some of these JDBC drivers are free; some
are not. Even if a JDBC driver is free, you also have
to ask whether the database to which it allows access
is free or not. Here are some inexpensive
possibilities:
- The JDBC/ODBC Bridge from
JavaSoft -- this is free, and comes with
JDK 1.1 and above. It allows access to a
large number of databases from (mostly)
Microsoft Windows clients, although ODBC
drivers are also available on various UNIX
systems. The databases you can connect to are
determined by the ODBC drivers you may have
on your system. Then you have to configure an
ODBC data source in order to use the
JDBC/ODBC Bridge. Configuration of a data
source is ODBC driver-specific, and on
Microsoft Windows is done through the ODBC
Administrator in the Control Panel
(Start->Settings->Control Panel). Note
the following restrictions with the JDBC/ODBC
Bridge:
- Currently, the only platforms
supporting it are Sun's Solaris and
Microsoft Windows
- The bridge does not work with
Microsoft J++ because it uses JNI
which Microsoft does not support.
- The bridge does not support the JDBC2
enhancements.
- The bridge is really only useful for
experimentation, not production
applications.
- InstantDB from ICS --
this is a Java-based database which is freely
downloadable.
- MiniSQL (mSQL) with the Imaginary JDBC Driver for
MinSQL
- IDS Server -- You can
download IDS
Server Lite for evaluation
- PointBase -- Another free
evaluation download
- Hypersonic SQL
- HyperSQL
- RmiJdbc -- a JDBC driver
that allows you to connect to a server using
RMI, and the server can then translate the
calls into local JDBC calls.
- In your Java program, load the chosen JDBC
Driver.
There are a number of ways of doing this:
- Explicitly call new
to load your driver's implementation of Driver.
For example:Driver driver = new <DriverImplementationClass>();
- Use the jdbc.drivers
property.
The DriverManager
will automatically load all classes listed in
the jdbc.drivers
System property.
For example:System.getProperties().put("jdbc.drivers",
"sun.jdbc.odbc.JdbcOdbcDriver:" +
"ORG.as220.tinySQL.textFileDriver:" +
"org.gjt.mm.mysql.Driver");
or the property can be set on the command
line:
(Note that the items in the property list
are separated by colons (':').
)
- Load the class using:
Class.forName(<driverImplementationClass>);
When you load a driver, by whatever mechanism, it
registers itself with the DriverManager.
- Get a connection using DriverManager and a
JDBC URL.
For example, here's how you typically do it:Connection conn = DriverManager.getConnection("jdbc:odbc:Employees", username, password);
There are three forms of the getConnection()
method:
public static Connection getConnection(String url);
public static Connection getConnection(String url,
String username,
String password);
public static Connection getConnection(String url,
Properties props);
(All three can throw a SQLException
on failure to get a connection.) This is because some
databases require a username and password to make a
connection to the database, while others do not.
Still others require a more complex set of
parameters.
A JDBC URL looks like this:
jdbc:<subprotocol>:<other-driver-specific-parameters>
- The URL protocol is jdbc
- You specify a subprotocol,
which a particular JDBC driver supports
- After the subprotocol, you specify a number
of driver-specific parameters.
For example, the JDBC/ODBC Bridge driver expects:
- A subprotocol of odbc
- Typically, the data source name
- Optionally, other ODBC-specific parameters
Once you've connected, you can access the database using
SQL statements.
Here's a simple example, which uses the JDBC/ODBC Bridge
driver:
package jdbc;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectToDatabase
{
public static void main(String[] args)
{
Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Employees");
}
catch(ClassNotFoundException ex)
{
// The Class.forName() failed to find the class
ex.printStackTrace();
}
catch(SQLException ex)
{
// The getConnection() failed
ex.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch(SQLException ex)
{ /* Do nothing */ }
}
}
}
} |
Note: You should be
cleaning up (i.e. closing) your connections explicitly, as in the above
example.
JDBC2 Connections and JNDI
In JDBC2, you can use the Java Naming and Directory
Interface (JNDI) to connect to a database, using code
such as:
Context jndiContext = ...;
DataSource dataSource = (DataSource)jndiContext.lookup("jdbc/myDatabase");
Connection conn = dataSource.getConnection(username, password);
which allows you to specify the data source name
externally, bypasses the DriverManager,
and simplifies the code.
We will find that a number of Enterprise Java components
use JNDI in similar ways. This is a definite trend.
|