20-07-2013, 04:37 PM
JDBC (Java Database Connectivity)
Java Database .ppt (Size: 295 KB / Downloads: 114)
Overview (1/2)
JDBC
JDBC is a standard interface for connecting to relational databases from Java
The JDBC Classes and Interfaces are in the java.sql package
JDBC is Java API for executing SQL statements
Provides a standard API for tool/database developers
Possible to write database applications using a pure Java API
Easy to send SQL statements to virtually any relational database
What does JDBC do?
Establish a connection with a database
Send SQL statements
Process the results
History of JDBC (1/2)
JDBC 1.0 released 9/1996.
Contains basic functionality to connect to database, query database, process results
JDBC classes are part of java.sql package
Comes with JDK 1.1
JDBC 2.0 released 5/1998
Comes with JDK 1.2
javax.sql contains additional functionality
Additional functionality:
Scroll in result set or move to specific row
Update database tables using Java methods instead of SQL commands
Send multiple SQL statements to the database as a batch
Use of SQL3 datatypes as column values
Loading a JDBC Driver
A JDBC driver is needed to connect to a database
Loading a driver requires the class name of the driver.
Ex) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver
Oracle driver: oracle.jdbc.driver.OracleDriver
MySQL: com.mysql.jdbc.Driver
Loaing the driver class
Class.forName("com.mysql.jdbc.Driver");
It is possible to load several drivers.
The class DriverManager manages the loaded driver(s)
Transactions and JDBC (1/2)
Transaction: more than one statement that must all succeed (or all fail) together
Ex) updating several tables due to customer purchase
If one fails, the system must reverse all previous actions
Also can’t leave DB in inconsistent state halfway through a transaction
COMMIT = complete transaction
ROLLBACK = cancel all actions
Summary
JDBC
Standard interface for connecting to relational databases from Java
Vendor independent API for accessing relational data
JDBC has four driver type
JDBC-ODBC bridge plus ODBC driver
Native-API partly-Java driver
JDBC-Net pure Java driver
Native Protocol pure Java API driver
JDBC support transaction and PreparedStatement