Java Reference
In-Depth Information
Loading a JDBC driver is very simple. It takes only one line of code. In our examples, we will
be using the JDBC-ODBC Bridge. The class name for this driver is
sun.jdbc.odbc.JdbcOdbcDriver . The following code snippet shows you how to load this dri-
ver:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
When you call the Class.forName() method, it creates an instance of the driver and registers it
with the DriverManager . This is all there is to loading a JDBC driver.
After you have the driver loaded, it is easy to make a connection. You make a call to the static
method DriverManager.getConnection() , which returns a connection to the database. Its
method signature is listed as follows:
public static synchronized Connection getConnection(String url,
String user, String password) throws SQLException
The first parameter is the URL that points to your data source. In the case of the JDBC-ODBC
Bridge, it always begins with jdbc:odbc: DataSourceName , where the DataSourceName is the
name of the ODBC data source you set up.
The next two parameters are self-explanatory. They are the username and password associated
with the database login. For this example, you will use empty strings for each. Here is the code
used to open a connection to your Movie Catalog database:
con = DriverManager.getConnection(“jdbc:odbc:Movie Catalog”,
“”, “”);
The Connection object returned from the DriverManager is an open connection to the data-
base. You will be using it to create JDBC statements that pass SQL statements to the database.
Performing the Basic SQL Commands
In this section we will look at how to create a JDBC Statement object and five JDBC exam-
ples that use the Statement object. In all the examples, you will be using the Movie Catalog
database that you configured earlier.
Creating a JDBC Statement Object
To execute any SQL command using a JDBC connection, you must first create a Statement
object. To create a statement, you need to call the Connection.createStatement() method. It
returns a JDBC statement that you will use to send your SQL statements to the database. The
following code snippet shows how to create a statement:
Statement statement = con.createStatement();
Search WWH ::




Custom Search