JDBC is a Java-based data access technology (Java Standard Edition platform) from Oracle Corporation. This technology is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment.
2. What is an API?
• application program interface
• a set of routines, protocols, and tools for building software
applications
• provides all the building blocks
3. JDBC API
• JDBC is a Java Database Connectivity API that lets you access virtually
any tabular data source from a Java application.
• JDBC defines a low-level API designed to support basic SQL
functionality independently of any specific SQL implementation. This
means the focus is on executing raw SQL statements and retrieving
their results.
• The JDBC 2.0 API includes two packages: java.sql, known as the JDBC
2.0 core API and javax.sql, known as the JDBC Standard Extension.
Together, they contain the necessary classes to develop database
applications using Java.
4. JDBC API (cont..)
• The JDBC Extension Package (javax.sql) was introduced to contain the parts of the
JDBC API that are closely related to other pieces of the Java platform that are
themselves optional packages, such as the Java Naming and Directory Interface
(JNDI) and the Java Transaction Service (JTS).
• The JDBC API, found in the java.sql package, contains only a few concrete classes.
Much of the API is distributed as database-neutral interface classes that specify
behavior without providing any implementation. The actual implementations are
provided by third-party vendors.
5. How JDBC API helps?
1. Open a Connection
2. Send a statement
3. Retrieve results
4. Close a connection 1. Create a connection
Session
2. Execute statement
3. Send results
4. Close the session
Java Application
DBMS Engine
Java DB API
JDBC API works as a connectivity helper between Java Application between DBMS Engine
6. Using JDBC API
To execute a statement against a database, the following flow is observed
• Load the driver (only performed once)
• Obtain a Connection to the database (save for later use)
• Obtain a Statement object from the Connection
• Use the Statement object to execute SQL. Updates, inserts and deletes return Boolean.
Selects return a ResultSet
• Navigate ResultSet, using data as required
• Close ResultSet
• Close Statement
• The same connection object can be used to create further statements, so donot close the
Connection
• But close the connection when no longer need to access the database
7. Classes and Interfaces of java.sql
Class or Interfaces Description
java.sql.Connection Create a connection with specific database
java.sql.DriverManager The task of DriverManager is to manage the database driver
java.sql.Statement It executes SQL statements for particular connection and retrieve
the results
java.sql.PreparedStatement It allows the programmer to create prepared SQL statements
java.sql.CallableStatement It executes stored procedures
java.sql.ResultSet This interface provides methods to get result row by row generated
by SELECT statements
8. Methods in JDBC API - Connection
Method Description
void close() This methods frees an object of type Connection from database and other
JDBC resources
void commit() This method makes all the changes made since the last commit or rollback
permanent. It throws SQLException.
Statement createStatement() This method creates an object of type Statement for sending SQL
statements to the database. It throws SQLException.
boolean isClosed() Return true if the connection is closed else return false.
CallableStatement
prepareCall(String S)
This method creates an object of type CallableStatement for calling the
stored procedures from database. It throws SQLException.
PreparedStatedment
prepareStatement(String S)
This method creates an object of type PrepareStatement for sending
dynamic SQL statements to the database. It throws SQLException.
Void rollback() This method undoes all changes mage to the database.
9. Methods in JDBC API - Statement
Method Description
void close() This methods frees an object of type Statement from database and other
JDBC resources
boolean execute(String S) This method executes the SQL statement specified by s. The
getResultSet() method is used to retrieve the result.
ResultSet getResultSet() This method retrieves the ResultSet that is generated by the execute()
method.
ResultSet
executeQuery(String S)
This method is used to execute the SQL statement specified by s and
returns the object of type ResultSet.
Int getMaxRows() This method returns the maximum number of rows those are generated
by the executeQuery() method.
int executeUpdate(String s) This method executes the SQL statement specified by s. The SQL
statement may be a SQL insert, update and delete statement.