0% found this document useful (0 votes)
69 views22 pages

JDBC Tutorial For M.SC (IT)

The document provides an overview of JDBC (Java Database Connectivity) including: - JDBC allows Java applications to access data from relational databases and provides interfaces for sending SQL statements and receiving results. - The JDBC architecture consists of the JDBC API layer and JDBC Driver API layer, with the driver manager ensuring the correct database driver is used. - Common JDBC components include the DriverManager, Driver, Connection, Statement, ResultSet, and SQLException classes. - There are four types of JDBC drivers: Type 1 (JDBC-ODBC bridge), Type 2 (native API), Type 3 (network), and Type 4

Uploaded by

Rakhi Manglani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views22 pages

JDBC Tutorial For M.SC (IT)

The document provides an overview of JDBC (Java Database Connectivity) including: - JDBC allows Java applications to access data from relational databases and provides interfaces for sending SQL statements and receiving results. - The JDBC architecture consists of the JDBC API layer and JDBC Driver API layer, with the driver manager ensuring the correct database driver is used. - Common JDBC components include the DriverManager, Driver, Connection, Statement, ResultSet, and SQLException classes. - There are four types of JDBC drivers: Type 1 (JDBC-ODBC bridge), Type 2 (native API), Type 3 (network), and Type 4

Uploaded by

Rakhi Manglani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

JDBC Tutorial

M.Sc (IT) Sem-IV

Prepared By Prof. Rakhi Budhrani


What is JDBC
• The JDBC API is a Java API that can access any
kind of tabular data, especially data stored in
a Relational Database.
• JDBC stands for Java Database Connectivity.
• JDBC works with Java on a variety of
platforms, such as Windows, Mac OS, and the
various versions of UNIX.

Prepared By Prof. Rakhi Budhrani


JDBC Architecture
JDBC Architecture consists of two layers:

• JDBC API: This provides the application-


to-JDBC Manager connection.

• JDBC Driver API: This supports the JDBC


Manager-to-Driver Connection.

• The JDBC API uses a driver manager and


database-specific drivers to provide
transparent connectivity to
heterogeneous databases.

• The JDBC driver manager ensures that


the correct driver is used to access each
data source. The driver manager is
capable of supporting multiple
concurrent drivers connected to multiple
heterogeneous databases.

Prepared By Prof. Rakhi Budhrani


Common JDBC Components
The JDBC API provides the following interfaces and classes:
• DriverManager: This interface manages a list of database drivers. Matches
connection requests from the java application with the proper database
driver using communication subprotocol.
• Driver: This interface handles the communications with the database
server. You will interact directly with Driver objects very rarely. Instead,
you use DriverManager objects, which manages objects of this type.
• Connection : The connection object represents communication context,
i.e., all communication with database is through connection object only.
• Statement : You use objects created from this interface to submit the SQL
statements to the database.
• ResultSet: These objects hold data retrieved from a database after you
execute an SQL query using Statement objects.
• SQLException: This class handles any errors that occur in a database
application.
Prepared By Prof. Rakhi Budhrani
The JDBC 4.0 Packages

The java.sql and javax.sql are


the primary packages for JDBC JDBC Driver
4.0. It offers the main classes
JDBC drivers enable you to
for interacting with your data
open database connections
sources.
and to interact with it by
sending SQL or database
commands then receiving
results with Java.

Prepared By Prof. Rakhi Budhrani


JDBC Drivers Types:
Type 1: JDBC-ODBC Bridge
Driver:
In a Type 1 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.
Prepared By Prof. Rakhi Budhrani
Type 2: JDBC-Native API:

In a Type 2 driver, JDBC API


calls are converted into native
C/C++ API calls which are
unique to the database.
These drivers typically
provided by the database
vendors and used in the same
manner as the JDBC-ODBC
Bridge, the vendor-specific
driver must be installed on
each client machine.

Prepared By Prof. Rakhi Budhrani


Type 3: JDBC-Net pure Java:

In a Type 3 driver, a three-tier


approach is used to accessing
databases. The JDBC clients
use standard network sockets
to communicate with an
middleware application server.
The socket information is then
translated by the middleware
application server into the call
format required by the DBMS,
and forwarded to the
database server.

Prepared By Prof. Rakhi Budhrani


Type 100: 100% pure Java:

In a Type 4 driver, a pure


Java-based driver that
communicates directly
with vendor's database
through socket connection.
This is the highest
performance driver
available for the database
and is usually provided by
the vendor itself.

Prepared By Prof. Rakhi Budhrani


Which Driver Should I use?
• If you are accessing one type of database, such as
Oracle, Sybase, or IBM, the preferred driver type is 4.
• If your Java application is accessing multiple types of
databases at the same time, type 3 is the preferred
driver.
• Type 2 drivers are useful in situations where a type 3 or
type 4 driver is not available yet for your database.
• The type 1 driver is not considered a deployment-level
driver and is typically used for development and
testing purposes only.
Prepared By Prof. Rakhi Budhrani
Steps for JDBC Connection
After you've installed the appropriate driver, it's time to establish a
database connection using JDBC.
The programming involved to establish a JDBC connection is fairly simple.
Here are these simple four steps:

• Import JDBC Packages: Add import statements to your Java program to


import required classes in your Java code.
• Register JDBC Driver: This step causes the JVM to load the desired driver
implementation into memory so it can fulfill your JDBC requests.
• Database URL Formulation: This is to create a properly formatted address
that points to the database to which you wish to connect.
• Create Connection Object: Finally, code a call to the DriverManager
object's getConnection( ) method to establish actual database connection.

Prepared By Prof. Rakhi Budhrani


Import JDBC Packages:
• The Import statements tell the Java compiler
where to find the classes you reference in your
code and are placed at the very beginning of
your source code.
• To use the standard JDBC package, which allows
you to select, insert, update, and delete data in
SQL tables, add the following imports to your
source code:
• import java.sql.* ; // for standard JDBC programs
Prepared By Prof. Rakhi Budhrani
Register JDBC Driver:
• You must register the your driver in your program before you use it.
Registering the driver is the process by which the Oracle driver's
class file is loaded into memory so it can be utilized as an
implementation of the JDBC interfaces.
• You need to do this registration only once in your program.
• to register a driver use Java's Class.forName() method to
dynamically load the driver's class file into memory, which
automatically registers it.

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

Prepared By Prof. Rakhi Budhrani


Database URL Formulation:
• After you've loaded the driver, you can establish a connection using the
DriverManager.getConnection() method.
getConnection(String url, String user, String password)
• Here each form requires a database URL. A database URL is an address
that points to your database.

static String userid="scott",password = "tiger";


static String url = "jdbc:odbc:Rakhi";

Here Rakhi is the DNS which you have created in the control pannel with the help
of ODBC
Create Connection Object:
Using a database URL with a username and password:
DriverManager.getConnection() method is used to create a connection object.
The most commonly used form of getConnection() requires you to pass a database
URL, a username, and a password:
con = DriverManager.getConnection(url, userid, password);

Prepared By Prof. Rakhi Budhrani


JDBC - Statements
• Once a connection is obtained we can interact with the database. The
JDBC Statement, CallableStatement, and PreparedStatement interfaces
define the methods and properties that enable you to send SQL or PL/SQL
commands and receive data from your database.

nterfaces Recommended Use


Use for general-purpose access to your database. Useful
Statement when you are using static SQL statements at runtime.
The Statement interface cannot accept parameters.
Use when you plan to use the SQL statements many
PreparedStatement times. The PreparedStatement interface accepts input
parameters at runtime.
Use when you want to access database stored
CallableStatement procedures. The CallableStatement interface can also
accept runtime input parameters.

Prepared By Prof. Rakhi Budhrani


Creating Statement Object:
Statement stmt = null;
try { Statement stmt = cn.createStatement();
stmt =
conn.createStatement( ); stmt.executeUpdate(insertString1);
...
} catch (SQLException e) { stmt.executeUpdate(insertString2);
...
} finally { stmt.executeUpdate(insertString3);
...
}
stmt.executeUpdate(insertString4);

insertString1 = "insert into Master


values(100,'SAL',30.5,'1-Dec-09',40.5)";

Prepared By Prof. Rakhi Budhrani


Prepared By Prof. Rakhi Budhrani
Prepared By Prof. Rakhi Budhrani
JDBC - Result Sets
• The SQL statements that read data from a database query
return the data in a result set. The SELECT statement is the
standard way to select rows from a database and view them
in a result set. The java.sql.ResultSet interface represents
the result set of a database query.
The methods of the ResultSet interface can be broken down
into three categories:
• Navigational methods: used to move the cursor around.
• Get methods: used to view the data in the columns of the
current row being pointed to by the cursor.
• Update methods: used to update the data in the columns of
the current row. The updates can then be updated in the
underlying database as well.

Prepared By Prof. Rakhi Budhrani


Select Query
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("Select * from
Master");
while(rs.next()){

System.out.println(rs.getInt(1) + "\t" +
rs.getString(2)+ "\t\t" +rs.getFloat(3)+"\t"
+rs.getDate(4)+"\t"+rs.getFloat(5));
}
Prepared By Prof. Rakhi Budhrani
Prepared By Prof. Rakhi Budhrani
Closing
• At the end of your JDBC program, it is required
explicitly close all the connections, statements,
recordsets to the database to end each database
session.
• rs.close();
• st.close();
• cn.close();
• For more details visit:
http://www.tutorialspoint.com/jdbc/index.htm
Prepared By Prof. Rakhi Budhrani

You might also like