Retrieving Data - a Simple JDBC Example
June 12, 2000
There are some distinct steps in accessing a database using JDBC.
STEP 1: Make a connection
This Step involves the driver setup and loading. Hence it is dependent
on the type of driver you are using. To make a connection with the
Database, we first
- Load the driver
- Get a connection using this driver
Load The driver
If you are using the Sun JDBC-ODBC bridge :
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
If you are using any other driver please refer to
Appendix A for a sample code for
loading Oracle driver. The code for loading any other JDBS driver
will be similar.
The forName Method will create an instance of a driver and register it
with the DriverManager.
Get a connection using this driver
The Generic code is:
Connection con = DriverManager.getConnection(url,
"myLogin", "myPassword");
url: If you are using JDBC-ODBC bridge then url will look like:
String url = jdbc:odbc:myDriver
Where myDriver is the Data Source Name of the ODBC driver to connect
to your database.
If you are using any other driver please refer to
Appendix A for an example of how
"url" will look.
The Connection returned (con) in the previous example is an 'open'
connection which we can use to send SQL data over.
JDBC a Brief Introduction
Building Web Applications Using Servlets and JSP Part II
STEP 2: Use This Connection to Access the Database
|