Nearby lessons

1 of 37

JDBC - Introduction

📌 What You Will Learn
  • Where JDBC fits in the Java world (Core vs Advanced Java)
  • JDBC in the simplest words — with a real-life story
  • Storage areas: temporary vs permanent
  • Why we use a database and not files
  • The evolution: C/C++ libraries → ODBC → JDBC
  • The difference between JDBC and ODBC

Introduction is a fundamental part of database programming with JDBC. This lesson explains First, Understand Core vs Advanced Java, JDBC in the Simplest Words and Storage Areas — Where Does Data Live? with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

First, Understand Core vs Advanced Java

With Core Java knowledge, we can build stand-alone applications — programs that run on one single machine. Examples: a calculator, MS Word.

With Advanced Java, we build web applications — programs that provide services over the internet. Examples: a mail service, a social media site, an online video site.

In Java, web applications are built using three main technologies, and each has its own job:

TechnologyJob in simple words
JSPShows things to the user (the view) — login page, inbox page, result page.
ServletDoes the thinking (the logic) — verify user, process data, talk to the database.
JDBCTalks to the database — sends SQL and brings back results.

So the flow is: JSP shows the page → Servlet does the logic → JDBC talks to the database. JDBC is the bridge between your Java code and the database.

In simple words: JDBC is the bridge between your Java code and the database. The Java program talks to JDBC, and JDBC carries the SQL to the database and brings the results back.

Java has three editions, and each serves a different purpose:

EditionFull nameUsed for
JSE / J2SEJava Standard EditionStand-alone (desktop) applications
JEE / J2EEJava Enterprise EditionWeb / enterprise applications
JME / J2MEJava Micro EditionMobile / embedded devices
Trainer's Note: Important: JDBC is part of JSE (Standard Edition), while Servlets and JSP are part of JEE (Enterprise Edition). So you can learn JDBC with just Core Java knowledge — no server needed.

JDBC in the Simplest Words

The classic material explains JDBC with a beautiful real-life story. Imagine a customer in one city wants to buy gold from a jewellery shop in another city. Four things are needed:

Story characterJDBC partWhat it does
TranslatorDriverConverts Java calls into database calls, and database calls back into Java. Without a translator, the two cannot understand each other.
RoadConnectionThe path on which the Java application and the database talk to each other.
VehicleStatementCarries the SQL query to the database and brings the results back.
Gold boxResultSetHolds the results returned by the SQL query.
In simple words: Driver = translator, Connection = road, Statement = vehicle, ResultSet = gold box. Keep this story in mind and the job of each JDBC part becomes easy to remember.

Remember this story and you will never forget what Driver, Connection, Statement and ResultSet do.

Storage Areas — Where Does Data Live?

Every application needs to store data — customer information, billing details, call records. Java gives us two kinds of storage areas:

TypeMeaningExamples
Temporary storageData stays only while the program runsJVM memory: heap, stack, method area
Permanent (persistent) storageData stays even after the program closesFile systems, databases, data warehouses

Temporary storage is cleared automatically when the JVM shuts down. Permanent storage keeps the data safe for later.

File Systems vs Databases

The simplest permanent storage is the file system (files on your computer). But files have serious limitations:

Problem with file systemsSimple words
Cannot store huge dataFiles get slow and messy with lots of data
No query languageSearching and updating data by writing code is very complex
No securityAnyone can read the file
No duplicate preventionThe same data can be saved twice → data inconsistency

Databases solve all these problems:

  • They can store huge amounts of information.
  • Every database has a query language (SQL) — so operations are easy.
  • Access needs a username and password — so data is secure.
  • Data is stored in tables with constraints (like primary key, unique key) — so duplicate data is prevented.

But even databases have limits: they cannot hold terabytes of data, and they handle only structured (table) data — not videos, audio or images comfortably. For that, big companies move to Big Data technologies and data warehouses.

What is JDBC?

JDBC (Java Database Connectivity) is a technology that lets a Java application communicate with any database.

In simple words: JDBC lets one Java program talk to any database. Write the code once, and it works with MySQL, Oracle or PostgreSQL — just by changing the driver and the URL.
  • JDBC is part of the Java Standard Edition.
  • JDBC is a specification defined by Sun Microsystems (now Oracle) and implemented by database vendors (Oracle, MySQL, PostgreSQL...). The vendor's implementation is called Driver software.

JDBC features worth remembering:

  • Database independent — write the application once, use it with any database.
  • Platform independent — JDBC drivers are written in Java, so they run on any operating system.
  • Easy CRUD — Create (Insert), Retrieve (Select), Update, Delete are all easy with JDBC.
  • Complex operations too — joins, stored procedures, and more.

JDBC versions and their Java versions:

JDBC versionPart of Java version
JDBC 3.0J2SE 1.4
JDBC 4.0Java SE 6
JDBC 4.1Java SE 7
JDBC 4.2Java SE 8
Trainer's Note: Updated knowledge: the modern versions (JDBC 4.x) are far easier than the old ones. From JDBC 4.0 (Java 6) onwards, the driver class is loaded automatically from the classpath — you no longer need to write Class.forName(...) yourself (though it still works). We will see this in Chapter 4.
📝 Key Takeaways
  • JDBC is part of Java SE and is the bridge between Java code and a database.
  • JSP shows pages, Servlets do logic, JDBC talks to the database.
  • Driver = translator, Connection = road, Statement = vehicle, ResultSet = gold box.
  • Permanent storage = files and databases; databases beat files on size, querying, security and consistency.
  • Evolution: database libraries → ODBC (Windows only) → JDBC (any platform).
  • JDBC is database independent and platform independent; JDBC 4.0+ auto-loads the driver.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5