JDBC API

JDBC API

 

JDBC API (Java Database Connectivity API) is a collection of classes and interfaces provided by Java that allows a Java program to communicate with databases.

The JDBC API provides standard methods for connecting to a database, executing SQL statements, retrieving results, and handling database errors. It allows Java programs to work with different databases without changing the basic JDBC programming approach.

The main JDBC API components are:

Component

Purpose

DriverManager

Establishes a connection with the database

Connection

Represents the connection between Java and the database

Statement

Executes SQL statements

PreparedStatement

Executes parameterized SQL statements

ResultSet

Represents data returned by a SELECT query

SQLException

Represents database-related errors

 

1.   DriverManager

DriverManager is a class in the java.sql package that manages JDBC drivers. It is mainly used to establish a connection between a Java application and a database. The getConnection() method is used with the database URL, username, and password. It identifies a suitable JDBC driver and returns a Connection object.

 

2.   Connection

Connection is an interface that represents an active connection between a Java application and a database. It is obtained using the DriverManager.getConnection() method. The Connection object is used to create Statement and PreparedStatement objects for executing SQL commands. After completing the database operations, the connection should be closed.

 

3.   Statement

Statement is an interface used to execute SQL statements from a Java program. A Statement object is created using the Connection object. Methods such as executeQuery() and executeUpdate() are used to execute SQL commands. It is commonly used when the SQL statement does not require dynamic input values.

 

4.   PreparedStatement

PreparedStatement is an interface used to execute parameterized SQL statements. It allows placeholders (?) to be used in an SQL statement and values to be supplied separately. Methods such as setInt() and setString() are used to provide the values. It is commonly used when SQL statements need to be executed with different input values.

 

5.   ResultSet

ResultSet is an interface that represents the data returned by a SELECT query. It contains the rows and columns retrieved from the database. The next() method is used to move through the rows one at a time. Methods such as getInt() and getString() are used to retrieve column values.

 

6.   SQLException

SQLException is an exception class used to represent errors that occur during database operations. It can occur while connecting to the database, executing SQL statements, or retrieving results. JDBC programs generally handle it using try-catch or throws. The exception provides information about the database error.

 

  

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu