Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Java JDBC overview - JDBC Tutorial

$
0
0

JDBC is the Java API for developing Java applications that access relational databases.

Why use JDBC

JDBC provides developers with a uniform interface to connect with different relational databases like Oracle, MySQL, DB2, Access etc.

JDBC provides a set of interfaces and classes that standardize the interaction with different databases and abstracts you as a developer with the inner working of the proprietary databases. You just need to know the standard JDBC steps to connect to database, query it in order to fetch results or update DB. Note here that the SQL may differ according to the DB used.

JDBC Driver

In Order to connect to database JDBC uses JDBC driver. Since JDBC driver acts as a connector between JDBC and proprietary databases JDBC drivers are DB specific and generally provided by the DB vendor itself.

As example– In order to connect to connect to MySql DB you will need a MySql JDBC connector driver which is bundled in the mysql-connector-javaXXX.jar.

The interaction of JDBC with the database using JDBC driver can be pictorially represented as follows -

JDBC Drivers

Packages in JDBC API

The JDBC API is comprised of two packages:

  • java.sql - Referred to as the JDBC core API
  • javax.sql - Referred to as the JDBC Optional Package API
You automatically get both packages when you download the Java Platform Standard Edition (Java SE).

Changes in JDBC 4.x

The current version of JDBC which comes bundled with Java 8 is JDBC 4.2. There are some noticeable changes in the 4.x versions like -

  1. Addition of the java.sql.SQLType Interface
  2. Addition of the java.sql.JDBCType Enum – Using SQLType interface and JDBCType Enum you can identify the generic SQL types like CLOB, REF_CURSOR, TINYINT, VARCHAR etc.
  3. You can use try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement.
  4. Automatic loading of JDBC drivers on the class path.

Steps for connecting to DB

JDBC API provides a set of interfaces and classes for connecting to DB, creating SQL statement, executing created SQL statement in database, returning the results and processing that ResultSet.

These steps can be summarized as follows -

  • Loading driver
  • Creating connection to DB
  • Creating Statement
  • Executing Query
  • Processing ResultSet
  • Closing connection

ReferJava JDBC Steps to Connect to DB to see these steps in details and a JDBC example Java program.

A pictorial representation of these steps can be represented as follows.

JDBC steps

That's all for this topic Java JDBC overview - JDBC Tutorial. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Types of JDBC Drivers
  2. Java JDBC Steps to Connect to DB
  3. Data access in Spring framework
  4. Select query using JDBCTemplate in Spring framework

You may also like -

>>>Go to Java advance topics page


Viewing all articles
Browse latest Browse all 938

Trending Articles