JDBC is a vital part of Java development when dealing with databases. It provides a standardized way to connect to and manipulate data in relational databases, making it an essential tool for building robust and data-driven Java applications.
Overview
- What is JDBC?
- Type of Drivers
- Steps to access
- Connection
- Statement
- CRUD operations
- Prepared Statement
- Callable statement
What Is JDBC?
- Java Data Base Connectivity is the Java API for accessing and
updating the databases. - Using JDBC, developers can interact with many databases like
Oracle DB, MongoDB, NoSQL etc, with the same piece of code. - Using different drivers, the java code interacts with the different
databases in a seamless manner.
Type of Drivers
- Type 1:- This is a JDBC-ODBC bridge that provides access to a database through an ODBC driver. This type of driver is slow and only appropriate for situations where no other JDBC driver is available.
- Type 2 :- This type is written partly in native-API and partly in Java. This type of driver uses the client API of the database to connect to the database.
- Type 3:- This type of driver translates JDBC calls into the middleware vendor’s protocol, which is then translated to the database access protocol by the middleware server.
- Type 4:- This type of driver is written in Java and connects to the database directly.
Steps To Access
- Load the JDBC driver.
- Obtain database connection.
- Create a Statement object.
- Create the ResultSet
- Close the JDBC objects.
Connection
- The Connection interface represents a connection to a database. You establish a connection using a JDBC driver’s URL, username, and password.
- Example:
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "username";
String password = "password";
Connection connection = DriverManager
.getConnection(url, username, password);
Connection Interface
- Connection is the session between the java application and the database.
- A factory of Statement, Prepared Statement etc.
- Provides transaction management mechanisms like commit, rollback.
- Some methods are createStatement(), commit(), rollback()
Statement Interface
- Statement interface provides the queries to interact with the
database. - Some of the methods are executeQuery(String),
executeUpdate(String), execute(String)
CRUD Operations
- Create Query :- statement.executeUpdate(“insert into emp values (.. , .. , .. )”)
- Retrieve Query :- statement.executeQuery (“select * from emp);
- Update Query :- statement.executeUpdate(“update emp set name =.. where id = .. “);
- Delete Query :- statement.executeUpdate(“delete from emp where id = .. “);
Prepared Statement
- Prepared Statements are used to improve the performance of the application as these use the parameterized strings, which just get compiled once.
- E.g. connection.prepareStatement(“update emp set name=? where id=?”);
Callable Statement
- CallableStatement interface is used for calling the stored procedures and functions from java programs.
- E.g. CallableStatement stmt=con.prepareCall(“{call myprocedure(?,?)}”);
- CallableStatement stmt=con.prepareCall(“{?= call myfunction(?,?)}”);