Hi, Hope you are doing well. This post is focused on JDBC-Java.
Let start with introduction.
What is JDBC?
JDBC stands for Java Database Connectivity
JDBC API provides universal data access from the Java.
Using the JDBC API, you can access virtually any data source, from relational databases(like MySQL ,Oracle, SQLite) to spreadsheets and flat files.
Discuss JDBC concept
Let us try to discuss from scratch were till now in the java domain we were simply writing codes(programming) and interacting with the database. Have you ever wonder imagining how internally our program can be linked to a database giving it enormous power to make projects and applications out of our program. Yes, it is possible with the help of this mechanism. Let us discuss in a real-world scenario which later on we will be mapping with technical terms to get an absolute technical understanding of the same with a pictorial representation.
Type of JDBC Drivers
JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:
JDBC-ODBC bridge driver
Native-API driver (partially java driver)
Network Protocol driver (fully java driver)
Thin driver (fully java driver)
Why Should We Use JDBC
Before JDBC, ODBC API was the database API to connect and execute the query with the database.
But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured).
That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can perform the following activities:
Connect to the database
Execute queries and update statements to the database
Retrieve the result received from the database.
How to make connect using JDBC?
Handling a connection requires following steps:
Load the driver
Open database connection
Close database connection
Let’s follow above steps in code:
1) Load JDBC driver
The easiest way to do this is to use Class.forName() on the class that implements the java.sql.Driver interface.
With MySQL Connector, the name of this class is com.mysql.jdbc.Driver. With this method, you could use an external configuration file to supply the driver class name and driver parameters to use when connecting to a database.
Class.forName("com.mysql.jdbc.Driver");
As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the “jdbc.drivers” system property. This allows a user to customize the JDBC Drivers used by their applications.
2) Open database connection
After the driver has been registered with the Driver Manager, you can obtain a Connection instance that is connected to a particular database by calling DriverManager.getConnection():
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "root", "password");
Once a Connection is established, it can be used to create Statement and Prepared Statement objects, as well as retrieve metadata about the database.
3) Close database connection
This step is as much important as opening a connection. Any connection left open is waste of resource and lead to various exceptions.
try
{
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
} catch (SQLException e) {
e.printStackTrace();
}
Complete JDBC Connection Example
Let’s see the whole thing working in an example below:
package com.codersarts.jdbc.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDemo {
public static void main(String[] argv) {
System.out.println("-------- MySQL JDBC Connection Example ------------");
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/Jdb_name", "root", "password");
System.out.println("SQL Connection to database established!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
return;
} finally {
try
{
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output:
-------- MySQL JDBC Connection Example ------------
MySQL JDBC Driver Registered!
SQL Connection to database established!
Connection closed !!
Some common task in JDBC?
CRUD Applications
Sorting and pagination
Developing simple and easy applications
How Codersarts can help you in JDBC?
Codersarts provide
JDBC assignment help
Help in JDBC Development projects
Error resolving in JDBC
Mentorship in JDBC from Experts
If you are looking for any kind of help in JDBC , Contact us
留言