This document provides instructions for creating a JDBC connection to a Microsoft Access database in Java. It discusses registering the testdb.mdb database with ODBC, registering the JDBC/ODBC bridge driver with the DriverManager, and includes examples of obtaining a Connection object. The key steps are: 1) registering the database with ODBC, 2) registering the JDBC driver with DriverManager via Class.forName(), and 3) obtaining a Connection using the jdbc:odbc:datasource URL and DriverManager.getConnection(). Code examples for connecting are provided in ConnectMe.java and ConnectMe1.java.
1. Prepared by-
Paneliya Prince (150120116047)
Guided by โ Prof.Prashant Jani
Gandhinagar Institute of Technology
SUBJECT-AJAVA(2160707)
Creating JDBC connection
2. 2
โข JDBC: Java Database Connectivity kit
โข Package: java.sql
โข DriverManager: class for making connection to
databases
โข Connection: interface for communicating with databases
once connection has been established.
โข Other interfaces: Statement, ResultSet,
DatabaseMetaData, ResultSetMetaData,
SQLException, โฆ.etc.
3. 3
Connecting to Databases with JDBC
โข JDBC-to-database communication path
Will discuss this path
using testdb.mdb
Textbook discusses this
path using testdb.mdb
Java Application
JDBC Driver Manager
JDBC/ODBC
bridge
Vendor
supplied
JDBC
driversODBC drivers
Database
4. 4
โข Register testdb.mdb with the ODBC Datasource Administer
on your PC. Instructions:
โ Open Control Panel and select ODBC Data Sources
[32bit] (on XP under Administrative Tools). You will
get the window shown on the right. Click on Add.
5. 5
โ Select Microsoft Access Driver and then click on Finish.
You will get the window shown on the right.
โ Provide a name for the data source, say TestDB.
โ Click and Select and select testdb.mdb from file system
(picture on the next page)
6. 6
โ Then you see the picture on the right. Simply click on OK
and the registration is complete.
โ Thereafter the database testdb.mdb is known as TestDB
to the ODBC Datasource Adminstrator.
โ For JDBC, URL for the database is: jdbc:odbc:TestDB
7. 7
โข Register JDBC driver (sun.jdbc.odbc.JdbcOdbcDriver )
with DriverManager
โ Method 1:
โข set system property jdbc.drivers
System.setProperty("jdbc.drivers",
"sun.jdbc.odbc.JdbcOdbcDriver");
โข At initialization, DriverManager loads all driver
classes referenced in jdbc.drivers
โ Method 2: Load driver explicitely at any time
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
A call to forName("X") causes the class named X
to be initialized.
Java Application
JDBC Driver Manager
JDBC/ODBC
bridge
Vendor
supplied
JDBC
drivers
ODBC
Database
8. 8
โข Connection examples
โ ConnectMe.java: register JDBC driver using the second
method
โ ConnectMe1.java: register JDBC driver using the first
method
โข Note. Examples (including testdb.mdb) in this topic are
packaged in the zip. See the code page