← Back to Chapters

JDBC API Packages

? JDBC API Packages

? Quick Overview

JDBC (Java Database Connectivity) API is a standard Java API that allows Java applications to interact with relational databases. It provides a set of packages and interfaces to connect, execute queries, and process results efficiently.

? Key Concepts

  • java.sql Core JDBC interfaces and classes
  • javax.sql Advanced JDBC features like DataSource
  • Driver management
  • Connection handling
  • Statement and ResultSet processing

? Syntax / Theory

The JDBC API is mainly divided into two important packages:

  • java.sql – Basic JDBC operations such as Connection, Statement, ResultSet
  • javax.sql – Enterprise-level features like connection pooling and RowSet

? Interactive Flow: JDBC Lifecycle

 
?Java App
⚙️Driver
?️Database
 

> System ready.

> Click 'Start Simulation' to begin JDBC flow.

? Code Example(s)

? View Code Example
// Loading JDBC driver and establishing database connection
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb","root","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close();

? Live Output / Explanation

What Happens Here?

  • The JDBC driver is loaded dynamically
  • A connection to the database is established
  • SQL query is executed using Statement
  • ResultSet fetches data row by row

✅ Tips & Best Practices

  • Always close Connection, Statement, and ResultSet
  • Use PreparedStatement to avoid SQL Injection
  • Prefer DataSource for large-scale applications

? Try It Yourself

  • Modify the query to fetch specific columns
  • Replace Statement with PreparedStatement
  • Connect JDBC with another database like Oracle