This document discusses how to create a JDBC connection to a database in Java. It explains that the Java Database Connectivity (JDBC) API uses the java.sql package and DriverManager class to connect to databases. It provides steps to register an Access database file as an ODBC data source, load the JDBC-ODBC bridge driver, and obtain a Connection object using the jdbc:odbc URL. Sample code files called ConnectMe.java and ConnectMe1.java are included to demonstrate explicitly and implicitly loading the JDBC driver class.
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
ย
creating JDBC connection
1. Prepared by-
Prince Paneliya (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