Nearby lessons
120 of 125Java - JDBC Basics
- What JDBC is and why we need it
- Database vs DBMS
- The modern steps to connect Java to a database
- Statement vs PreparedStatement vs CallableStatement
- Running queries and reading results (CRUD)
- Transactions — making changes safe
JDBC Basics is a core concept of the Java language. This lesson explains What is JDBC?, Database vs DBMS and The Five Steps of JDBC (Updated for Modern Java) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is JDBC?
JDBC (Java Database Connectivity) is Java's standard way for a program to connect to a database, send SQL commands, and read the results.
Think of JDBC as the bridge between your Java program and the database. Your program sends SQL like SELECT * FROM student through JDBC, the database answers, and JDBC brings the rows back to Java.
Everything lives in the java.sql package.
Database vs DBMS
| Term | Meaning | Example |
|---|---|---|
| Database | The organised collection of data (the tables themselves) | student_data, employee_data |
| DBMS / RDBMS | The software that stores, manages and serves that data | MySQL, Oracle, PostgreSQL |
You will hear database used loosely for both. Technically: MySQL is the DBMS software; inside it you create databases (collections of tables).
The Five Steps of JDBC (Updated for Modern Java)
The old books (like the one this material is based on) used the JDBC-ODBC bridge driver, which has been removed from modern JDK. Today we use the Type 4 driver provided by the database vendor (for MySQL: com.mysql.cj.jdbc.Driver). Here are the modern five steps:
- Load the driver — register the database's driver class.
- Get a Connection — use DriverManager.getConnection(url, user, password).
- Create a Statement — prepare an object to carry your SQL.
- Execute the query — run the SQL and (if any) get results.
- Close the resources — close ResultSet, Statement and Connection (use try-with-resources).
Understanding the URL
The connection URL is just an address. Let us read it piece by piece:
The Three Kinds of Statements
| Statement type | Best for | Important points |
|---|---|---|
| Statement | Simple SQL run once | SQL written as text; risk of SQL injection |
| PreparedStatement | SQL run with values, repeated or with user input | Recommended — uses ? placeholders, prevents SQL injection, faster |
| CallableStatement | Calling stored procedures in the database | Uses {call procedureName(?, ?)} |
CRUD — The Four Basic Operations
CRUD = Create, Read, Update, Delete. Every database application is basically these four operations. Here is a complete example using PreparedStatement (modern style):
executeQuery vs executeUpdate
| Method | Used for | Returns |
|---|---|---|
| executeQuery(sql) | SELECT statements | A ResultSet (the rows) |
| executeUpdate(sql) | INSERT, UPDATE, DELETE | An int (how many rows changed) |
| execute(sql) | Any SQL (unknown type) | boolean (is it a ResultSet?) |
ResultSet — Reading the Rows
A ResultSet holds the rows returned by a SELECT. rs.next() moves to the next row (returns false when finished). Then we read each column by name or index:
Transactions — All or Nothing
A transaction is a group of changes that must all succeed together. For example, transferring money from one account to another — if the 'add' succeeds but the 'remove' fails, the money is wrong. Transactions protect us:
Database Software — What Students Use
| Database | Good for | Driver class (Type 4) |
|---|---|---|
| MySQL | Most common for students and free | com.mysql.cj.jdbc.Driver |
| Oracle | Big companies, advanced features | oracle.jdbc.OracleDriver |
| PostgreSQL | Free, powerful, modern | org.postgresql.Driver |
| H2 / SQLite | Small projects, no installation | org.h2.Driver / org.sqlite.JDBC |
Database vs Database Management System — Deep Dive
The classic material asks this question in detail. There are also three types of DBMS — know the difference:
| Term | What it is | Example |
|---|---|---|
| Database | A memory area that stores organised data (the tables). | student_data, employee_data |
| DBMS | Software that manages the database — stores, retrieves, updates. | MySQL, Oracle |
| RDBMS | DBMS that stores data in tables with relations between them (uses primary keys and foreign keys). | MySQL, Oracle, PostgreSQL |
So the chain is: Database = the data; DBMS = the software managing it; RDBMS = the special DBMS where data lives in related tables (this is what JDBC uses).
The classic material also mentions two related ideas:
- Data warehousing — storing huge amounts of historical data for analysis; it uses data mining techniques for fast retrieval.
- SQL (Structured Query Language) — the language we use to talk to the DBMS. JDBC simply sends SQL to the DBMS and brings back results.
- JDBC is the bridge between Java and a database.
- Five steps: load driver, get connection, create statement, execute SQL, close resources.
- PreparedStatement with ? placeholders is the safe, professional choice (prevents SQL injection).
- executeQuery returns ResultSet; executeUpdate returns row count.
- CRUD = Create (INSERT), Read (SELECT), Update (UPDATE), Delete (DELETE).
- Transactions: setAutoCommit(false) + commit/rollback for all-or-nothing changes.
- Modern drivers are Type 4 vendor drivers (the old JDBC-ODBC bridge is removed).