JDBC
Introduction to JDBC (Java Database Connectivity)
- JDBC, which stands for Java Database Connectivity, is a Java-based technology that facilitates
communication between Java applications and databases.
- Contrary to being a programming language, JDBC is a technology that provides a set of Java
classes and interfaces for developers to interact with databases, enabling the creation of
robust and dynamic applications.
- Purpose of JDBC:
- JDBC serves as a crucial mediator between a Java application and a database, allowing
seamless communication and data exchange. This technology is versatile and can be
employed in various types of Java applications, including but not limited to applets,
servlets, and JSP (JavaServer Pages).
- Supported Databases: JDBC is compatible with a wide range of databases such as Oracle,
SQL Server, MySQL, and more, making it a universal solution for database connectivity in
Java-based projects.
- Features of JDBC:
- Standard API:
- API (Application Programming Interface): In the context of JDBC, an API refers to a set of rules and protocols that allow different software applications to communicate with each other. JDBC provides a standard API for Java programs to interact with relational databases.
- Database Independence:
- JDBC is designed to be a database-independent API. This means that once a JDBC program is developed for a specific database (e.g., Oracle), it can be used with other databases (e.g., SQL) with minimal or no modifications. Typically, only adjustments to the connection details are required.
- Platform Independence:
- JDBC is built on Java, a platform-independent technology. Consequently, JDBC programs can be executed on various operating systems, such as Windows, Linux, macOS, or Unix, without the need for modification. This inherent portability is a result of Java's "write once, run anywhere" philosophy.
- CRUD and SCUD Operations:
- JDBC facilitates the fundamental database operations: Create, Retrieve, Update, and Delete (CRUD). Additionally, it supports SCUD operations, which involve Select, Create, Update, and Deletion operations, covering a comprehensive range of interactions with a database.
- Support for Advanced SQL Concepts:
- JDBC enables developers to work with advanced SQL concepts such as views, triggers, cursors, and stored procedures. This allows for more sophisticated and customized interactions with the underlying database system.
- Key Components of JDBC:
- Driver: JDBC drivers play a vital role in the interaction between Java
applications and databases. They are platform-specific implementations that enable the
translation of Java calls into database-specific calls.
- Connection: JDBC facilitates the establishment of a connection to the
database using the DriverManager class. This connection is essential for executing SQL
queries and transactions.
- Statement: JDBC provides various statement interfaces, such as
Statement, PreparedStatement, and CallableStatement, to execute SQL queries and updates
against the database.
- ResultSet: The ResultSet interface allows Java applications to retrieve
and process the results of SQL queries from the database.
- Benefits of JDBC:
- Portability: JDBC's platform independence ensures that Java
applications can seamlessly connect to different databases without significant code
modifications.
- Scalability: JDBC supports scalable database access, making it suitable
for projects ranging from small-scale applications to large enterprise systems.
- Security: JDBC provides mechanisms for secure database transactions,
helping developers implement robust security measures in their applications.
JDBC operations using MySQL
- Mainly we can perform 4 operations: (CRUD)
- Create
- Retrieve
- Update
- Delete
We will understand these operations using a program
import java.sql.*;
public class CRUD
{
public static void main(String[] args) throws Exception
{
Class.forName("com.mySql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "Root1234@");
// now we will perform first operation which is creating a table
Statement st = con.createStatement();
st.executeUpdate("create table student(rno int, name varchar(20), branch varchar(20))");// using this we are creating a table
// closing Statement and Connection object
st.close();
con.close();
}
}
- java.sql package contains several classes and interfaces which helps
- Inside the main function as we use several methods like get connection and those methods throws some exception so we have to handle the exception using try catch or explicitly throw them
- Class.forName("com.mydql.jdbc.Driver"); - Class is a static class which is available in lang package, there is no need to import the lang package and forName is a static method which is used to load corresponding driver class. Driver class is available in jdbc folder, jdbc is availabe in mysql, mysql folder is available in com folder. ForName method throws class not found exception which can be handle by throws sqlexception
- Connection is an Interface and we know that we can't create object of an interface so in order to create an object we are using getconnection method, getconnectio is a static method which is available in driver manager class. In getConnection method we have to specify three arguments 1st one is to specify the url of the mysql using which we connection to mysql. Default portnumber for mysql is 3306 after this we specify databse name. 2nd argument is username and then 3rd is the password.
- In order to create a table first we have to create Statement object, Statement is an interface and we create its object using createStatement method we can call createStatement method using Connection object. Create operation is done using executeUpdate