Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
(JDBC)
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Textbook
Chapter 7
What is JDBC? Internal format of databases and its associated database API vary
from vendor to vendor
SQL and Versions of JDBC
Internal format of an Oracle database differs from that of Access
ODBC & JDBC-ODBC
database, while the format of MySQL database differs from both
Simple Database Access of these.
Modifying the Database Contents Coping with these variation of internal format is the fundamental
problem
Transactions
Meta Data Attempting to provide a general access method that will work for
all relational databases is the issue of concern
Scrollable ResultSets in JDBC 2.0
This issue of connecting java program to any DB is addressed
Modifying Databases via Java Methods using the DB driver classes, providing vendor-specific API for that
database
It is a mediating module that allows JDBC to communicate with
vendor-specific API for that database
The Vendor Variation Problem Is a standard interface for connecting to relational databases from
Java
What is JDBC?
Contains collection of API’s that lets you access virtually any
SQL and Versions of JDBC
tabular data source from the Java programming language
ODBC & JDBC-ODBC
Information is transferred from relations to objects and vice-versa
Simple Database Access databases optimized for searching/indexing
Modifying the Database Contents objects optimized for engineering/flexibility
Transactions A JDBC driver is a set of Java classes that implements JDBC interfaces,
targeting a specific database
Meta Data
Oracle
These are Driver
Java classes
Oracle
API calls
Java Web/
Standalone JDBC MS SQL
Application Driver
MS SQL
MySQL
Driver
Network MySQL
JDBC GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 6
JDBC The standard means of accessing a relational database is to use SQL
The Vendor Variation Problem (Structured Query Language)
What is JDBC? JDBC 1.0 package, appeared in JDK 1.1, contains the core JDBC API in
java.sql package
SQL and Versions of JDBC
ODBC & JDBC-ODBC Using this API, it is possible to access data from relational databases,
spreadsheets and flat files
Simple Database Access
Provides DriverManager class for making a connection to DB
Modifying the Database Contents
Transactions
With JDBC 2.0 in J2SE 1.4, extra functionality was introduced with the
additional package javax.sql
Meta Data
DataSource interface was introduced that provides the preferred method
Scrollable ResultSets in JDBC 2.0
of making a connection to a database
Modifying Databases via Java Methods
A DataSource object has properties that can be modified.
Ex.: If the data source is moved to a different server, the property for the
server can be changed without requiring the code accessing the data
source to be changed
Transactions
Meta Data
Given a URL, DriverManager looks for the driver that can talk to the corresponding database
jdbc:mysql://192.168.2.1:3306/myDB
SQL DML (Data Manipulation Language) statements change the contents of the database (viz., INSERT, DELETE and UPDATE
statements).
executeUpdate() method of Statement class executes DML
returns an integer indicating the number of database rows affected by the updating operation.
Ex.:
Modifying the Database Contents String query = "UPDATE Accounts SET surname = 'Bloggs’, firstNames = 'Fred Joseph’
WHERE acctNum = 123456";
Transactions
int result = stmt.executeUpdate(query);
Meta Data
What is JDBC? commit is used at the end of a transaction to commit/finalize database changes
SQL and Versions of JDBC rollback is used to restore the database to the state it was in prior to the current
transaction
ODBC & JDBC-ODBC
rs.absolute(2)
rs.updateInt(“acctNum”, 2547);
rs.updateRow();
…
rs.moveToInsertRow();
rs.updateInt("acctNum", 999999);
rs.updateString("surname", "Harrison");
rs.updateString("firstNames", "Christine Dawn");
rs.updateFloat("balance", 2500f);
rs.insertRow();
…
results.absolute(3); //Move to row 3.
results.deleteRow();
JDBC GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 30
End of JDBC