JDBC a Brief Introduction
June 12, 2000
JDBC (Java database connectivity) is an API for database access from
within a Java program. You can use JDBC to access any database that
supports the API by providing a JDBC driver.
JDBC is similar to ODBC in the sense that it is database independent.
For more information on JDBC Check out
the Sun JDBC Product page.
There are two most commonly used versions of JDBC API. The JDBC 1.2 API
and the more recently released JDBC 2.0 API. There are some distinct
advantages of using JDBC 2.0. We will cover that a little later. First
let's see how to write a simple program using the common features of
JDBC APIs
To use the JDBC API to access any database you need a JDBC driver.
These drivers are either written purely in Java, or use the Java Native
Interface.
Here
is the list of different drivers available.
For our example we will use the JDBC-ODBC bridge available from SUN.
(Please Refer to Appendix A for more
instructions on installing an Oracle JDBC driver). If you have JDK1.2,
you probably have the JDBC-ODBC bridge with you!
If you are using any other JDBC driver, please follow your driver
vendor's instructions to install the driver.
If you want to use the JDBC - ODBC bridge, all you need to do is to
create an ODBC driver to connect to your database.
NOTE: We will assume that you have the following set up.
- A database with a sample table named USERTABLE with the following
fields USERID char(5) and PASSWORD char(5). An example of a create
statement is:
CREATE TABLE USERTABLE
(
USERID CHAR(5),
PASSWORD CHAR(5)
);
You can insert a record in this table with
Insert into USERTABLE (USERID , PASSWORD)
Values('guest', 'guest');
- An ODBC driver to connect to this database.
We will assume the DSN (data source) name as myDriver
Warning The execution of the sample code will depend on the
driver and the driver set up. It is important that you understand how
to install a database driver (JDBC and / or ODBC) and how to connect
to your database using this driver.
Please Refer to Appendix A
for more instructions on installing the JDBC and ODBC drivers.
Building Web Applications Using Servlets and JSP Part II
Building Web Applications Using Servlets and JSP Part II
Retrieving Data - a Simple JDBC Example
|