E-Note 21146 Content Document 20240622100933PM
E-Note 21146 Content Document 20240622100933PM
It means that the JDBC allows to construct SQL statements and embed
them inside Java API calls.
JDBC Driver is a software component that enables java application to interact with
the database. There are 4 types of JDBC drivers:
Also known as
1. Type-1: JDBC-ODBC Bridge Driver, 1. Type 1: JDBC-ODBC Bridge Driver
2. Type-2: Native Driver, 2. Type 2: JDBC-Native API
3. Type-3: Network Protocol Driver or JDBC
3. Driver,
Typeand3: JDBC-Net Pure Java
4. Type-4: JDBC Driver 4. Type 4: 100% Pure Java
Type 1: JDBC-ODBC Bridge Driver
➢ a JDBC bridge is used to access ODBC (open Database
Connectivity) drivers installed on each client machine.
➢ Using ODBC, requires configuring on your system a Data Source
Name (DSN) that represents the target database
Type 1: JDBC-ODBC Bridge Driver
• a JDBC bridge is used to access ODBC drivers installed on each client machine.
• Using ODBC, requires configuring on your system a Data Source Name (DSN) that
represents the target database
• The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC
function calls. This is now discouraged because of thin driver.
Type 2: JDBC-Native API
➢ JDBC API calls are converted into native C/C++ API calls, which are unique to
the database.
➢ These drivers are typically provided by the database vendors
Type 2: JDBC-Native API
Disadvantage:
➢ The native driver needs to be installed on the each client
machine
➢ Loss of code portability
Type 3: JDBC-Net Pure Java/ JDBC Driver
a three-tier approach is used to access databases
The JDBC clients use standard network sockets to communicate with a middleware
application server.
Type 3: JDBC-Net Pure Java/ JDBC Driver
➢ The thin driver converts JDBC calls directly into the vendor-specific
database protocol.
➢ That is why it is also known as thin driver. It is fully written in Java
language.
Which Driver should be Used?
1. The first package is called java. Sql and contains core Java data objects of the JDBC
API.
➢ These include Java data objects that provide the basics for connecting to the
DBMS and interacting with data stored in the DBMS.
➢ java .sql is part of the J2SE.
2. The other package that contains the JDBC API is javax.sql, which extends java.sql
and is in the J2EE.
➢ Included in the javax.sql package are Java data objects that interact with Java
Naming and Directory Interface (JNDI) and Java data objects that manage
connection pooling, among other advanced JDBC features.
Connecting to Database:
Import JDBC Packages
❖ DriverManager.registerDriver()
when using a non-JDK compliant JVM, such as the one provided by Microsoft.
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
Create Connection Object
1. If you have a host at TCP/IP address 192.0.0.1 with a host name of myhostname, and
2. your Oracle listener is configured to listen on port 1521, and
3. your database name is EMP,
then complete database URL would be:
jdbc:oracle:thin:@myhostname:1521:EMP
String URL = "jdbc:oracle:thin:@myhostname:1521:EMP";
String USER = "username";
String PASS = "password“;
Connection conn = DriverManager.getConnection(URL, USER, PASS);
Using Only a Database URL
jdbc:oracle:driver:username/password@database
jdbc:oracle:thin:username/password@amd:1521:EMP
Closing JDBC Connections
conn.close();
Java Database Connectivity with 5 Steps
• Register the Driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
• Create connection
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@local
host:1521:xe","system","password");
• Create statement
Statement stmt=con.createStatement();
• Execute queries
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
• Close connection
con.close();
Database Connection
▪ A J2EE component does not directly connect to a DBMS. Instead, the J2EE component connects
with the JDBC driver that is associated with the DBMS.
▪ However, before this connection is made, the JDBC driver must be loaded and registered with
the DriverManager.
▪ The purpose of loading and registering the JDBC driver is to bring the JDBC driver into the Java
Virtual Machine (JVM). The JDBC driver is automatically registered with the DriverManager once
the JDBC driver is loaded and is therefore available to the JVM and can be used by J2EE
components.
▪ The Class.forName() method is used to load the JDBC driver.
▪ In this example, the JDBC/ODBC Bridge is the driver that is being loaded. You can replace the
JOBC/ODBC Bridge with the appropriate JDBC driver for the DBMS being used in your J2EE
application.
▪ The Class.forName() method throws a ClassNotFoundException if an error occurs when loading
the JDBC driver. Errors are trapped using the catch{} block whenever the JDBC driver is being
loaded.
34
Dept. of CSE, DSU
The Connection Object
▪ After the JDBC driver is successfully loaded and registered, the J2EE component
must connect to the database. The database must be associated with the JDBC
driver, which is usually performed by either the database administrator or the
systems administrator.
▪ The data source that the JDBC component will connect to is defined using URL
format. The URL consists of three parts. These are
▪ JDBC which indicates that the JDBC protocol is to be used to read the URL
▪ <subprotocol> which is the JDBC driver name.
▪ < subname> which is the name of the database.