Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

JDBC API

• When we use JDBC API into a Java Program is called as JDBC Program.
• JDBC API provides 2 different packages:

a. java.sql package
b. javax.sql package

• In the above packages we get various classes of interfaces.

Frequently used Classes or Interfaces of java.sql package: -

a. java.sql.DriverManager class : - java.lang.DriverManager class is an important component of JDBC API. This class is basically responsible to register the JDBC Driver and establish the connection between JDBC Program to Database.
b. java.sql.Driver interface : - Provides the API for registering and connecting drivers based on JDBC technology ("JDBC drivers"); generally used only by the DriverManager class.
c. java.sql.Connection interface :- This Interface provides method to create statement and managing connections.
d. java.sql.Statement interface :- In this interface we get different methods to submit the SQL query to the Database.
e. java.sql.PreparedStatement interface: - Using PreparedStatement methods we send the precompiled SQL queries to the Database.
f. java.sql.CallableStatement interface: - Using CallableInterface we call the procedures of the database from JDBC Program.
g. java.sql.ResultSet interface: - Using ResultSet type object we represents the fetched database records.
h. java.sql.DatabaseMetaData: - Using DatabaseMetaData interface type object we get Comprehensive information about the database as a whole.
i. java.sql.ResultSetMetadata: - An object that can be used to get information about the types and properties of the columns in a ResultSet object.

Javax.sql package uses: -

This Package Provides the API for server side data source access and processing from the Java programming language. This package supplements the java.sql package.
The javax.sql package provides for the following:

a. The DataSource interface as an alternative to the DriverManager for establishing a connection with a data source.
b. Connection pooling and Statement pooling.
c. Distributed transactions.
d. Rowsets.

• Steps to write a JDBC Program:

Step 1: Load the driver's class.
Step 2: Establish the connection between the JDBC program and the database.
Step 3: Prepare the SQL Statement.
Step 4: Create the JDBC Statement.
Step 5: Submit the SQL statement to the database using a JDBC statement.
Step 6: Process the result.
Step 7: Close all the resources.

            
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Jtc1{
public static void main(String[] args) throws Exception{
// Step-1 : Loading the Oracle Database Driver Class.
    Class.forName("com.mysql.cj.jdbc.Driver");
// Step-2 : Establishing the connection to the Database.
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tutorial", "root", "root");
// Step-3 : Preparing the SQL Statement.
    String sql = "insert into JtcStudent values(101,'Vivek Sareen','viveksareen@jtcindia.org','Noida')";
// Step-4 : Preparing the JDBC Statement.
    Statement statement = connection.createStatement();
// Step-5 : Submit the SQL statement to the Database using a JDBC statement.
    int a = statement.executeUpdate(sql);
// Step-6 : Processing the Result.
    if(a != 0) {
        System.out.println("Data Inserted Successfully...");
    }else {
        System.out.println("Pleses Try Again...");
    }
// Step-7 : Closing Resources.
        connection.close();
    }
}
        

In the above we are injecting the Data of a JTC Student into Oracle Database Table JtcStudent.

Note: - To Connect JDBC Application to Oracle Database ojdbc14.jar file is required to load first.