Documente Academic
Documente Profesional
Documente Cultură
1
Introduction
Different database systems have their own API that
you must learn to write programs that interact with
the database
2
JDBC Basics
JDBC-Stands for Java Database Connectivity
An API, or Application Programming Interface is a
set of classes, methods, and resources that
programs can use to do their work
JDBC is a Java API for database connectivity, that
defines an interface for accessing various databases
3
JDBC Basics
Nearly all relational database management systems
(RDBMSs) support SQL, and Java is platform
independent.
4
JDBC Basics
In short JDBC helps the programmers to write java
applications that manage these three programming
activities:
Connect to a data source, like a database.
5
JDBC Architecture
6
JDBC Architecture
The JDBC API uses a driver manager and database-specific
drivers to provide transparent connectivity to heterogeneous
databases
8
Driver:
JDBC Drivers are written by vendors and must support the basic
features of the JDBC specification.
9
JDBC Drivers
JDBC drivers are client-side adapters (installed on the client
machine, not on the server) that convert requests from Java
programs to a protocol that the DBMS can understand.
10
Type 1 - JDBC-ODBC Bridge
11
Type 1 - JDBC-ODBC Bridge
The JDBC-ODBC Bridge, is a database driver that
utilizes the ODBC driver to connect the database.
Type 2
14
Type 2 - JDBC-Native Bridge Driver
This driver converts JDBC calls into database-specific
calls.
15
Type 2 - JDBC-Native Bridge Driver
Like Type 1 drivers, Type 2 drivers require native
database client libraries to be installed and configured
on the client machine.
17
Type 3 - JDBC-Net Bridge
It makes use of a middle-tier between the calling program
and the database
20
Type 4 - Direct JDBC Driver
Type 4 drivers are pure Java drivers that converts
JDBC calls directly into the vendor-specific database
protocols
22
Which Driver should be used?
23
Package java.sql :Classes
It contains a set of interfaces and classes defined by JDBC
API that are used for communicating with the database
Important classes
DriverManager
The JDBC Driver Manager is a very important class that
defines objects which connect Java applications to a
JDBC driver
Date
The Date class is used to represent dates in the SQL
format YYYY-MM-DD
• Time
The Time class is used to represent times in the SQL
format HH:MM:SS
Types
provides definitions for all of the SQL data types that can
be supported by a JDBC driver
26
Interfaces in java.sql
Driver
All JDBC Drivers must implement the Driver interface.
Each driver should supply a class that implements
the Driver interface.
Connection
Represents a particular connection on which further
actions will be allowed
Statement
Used for executing SQL statements against the database
and retrieve data 27
Interfaces in java.sql
PreparedStatement
Creates a precompiled SQL statement and store it in the
database
Pre-compilation allows more faster & efficient statement
ResultSet
Represents the result of an SQL statement
Provides methods for navigating through the resulting
data
28
Interfaces in java.sql
CallableStatement
Used for executing stored procedures
ResultSetMetaData
Gives information like type and properties of the columns in a
result set
DatabaseMetaData
Provides information about the database as a whole
29
Using JDBC
Seven steps in using JDBC to access a database:
1. Importing java.sql package
2. Load the driver (Only performed once)
3. Establishing a Connection to the database (Save for later
use)
4. Obtain a Statement object from the Connection
5. Use the Statement object to execute SQL. [Updates, inserts
and deletes return Boolean. Selects return a ResultSet]
6. Retrieve and Navigate through ResultSet, using data as
required
7. Close Statement and connection
30
Using JDBC
31
Loading a driver
32
Connecting to the DBMS
Once the driver is loaded, connect to the DBMS using method
DriverManager.getConnection()
.
33
Connecting to the DBMS
The DriverManager.getConnection() method is passed the
URL of the database, and the userID and password if
required by the DBMS
Eg:
String url =“jdbc:odbc:CustomerInformation”;
String userID=“sjec”;
String password = “comp”
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDrive”);
Connection conn =
DriverManager.getConnection(url,userID,password);
}
This method returns a Connection interface that is used
throughout the process to reference the database.
34
Creating and executing a statement.
Statement object is used to send queries and commands to the
database and is created from the Connection as follows:
35
Creating and executing a statement.
Statement object is used to send queries and commands to the
database and is created from the Connection as follows:
37
ResultSet:Navigational Methods
boolean next()-Moves the cursor to the next row
38
ResultSet: Get Methods
The ResultSet interface contains dozens of methods for
getting the data of the current row.
41
Prepared Statement
A prepared statement is an SQL statement that contains
parameters.
44
Java Example
import java.sql.*
public class JDBCSample {
public static void main( String args[]) {
String userName = "testuser";
String password = "testpass";
String url = "jdbc:mysql://localhost/test";
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (url, userName,
password);
45
//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();
//Execute the SQL statement and get the results in a
Resultset
ResultSet rs = stmd.executeQuery("select moviename,
releasedate from movies");
// Iterate through the ResultSet, displaying two values
// for each row using the getString method
while (rs.next())
System.out.println("Name= " + rs.getString("moviename") +
" Date= " + rs.getString("releasedate");
}
46
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connection
con.close();
}
}
}