B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP JDBC Tutorial in Java - Connect to Database with Examples

Introduction to JDBC in Java

JDBC (Java Database Connectivity) is a Java API that allows Java applications to interact with relational databases such as MySQL, PostgreSQL, Oracle, and more. It enables you to perform SQL operations like SELECT, INSERT, UPDATE, and DELETE directly from your Java code.

JDBC acts as a bridge between your Java program and the database server, allowing you to connect, query, and manage data easily and securely.

  • Establish a database connection using DriverManager
  • Execute SQL queries using Statement or PreparedStatement
  • Retrieve data using ResultSet
  • Handle exceptions and manage transactions efficiently

JDBC (Java Database Connectivity) एक Java API है जो Java प्रोग्राम को रिलेशनल डेटाबेस जैसे MySQL, PostgreSQL, और Oracle से कनेक्ट करने में मदद करता है। इसकी मदद से आप SELECT, INSERT, UPDATE, DELETE जैसे SQL ऑपरेशन्स कर सकते हैं।

Prerequisites

Before running JDBC programs, make sure you have the following installed and configured:

SQL to create database and table:
CREATE DATABASE studentdb;
USE studentdb;

CREATE TABLE students (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100)
);

INSERT INTO students (name) VALUES ('Aryan'), ('Riya'), ('Amit');
Sample Data in students Table
+----+--------+
| id | name   |
+----+--------+
| 1  | Aryan  |
| 2  | Riya   |
| 3  | Amit   |
+----+--------+

Example: Connect to MySQL using JDBC

import java.sql.*;

public class JdbcExample {
  public static void main(String[] args) {
    try {
      Class.forName("com.mysql.cj.jdbc.Driver");
      Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/studentdb", "root", "your mysql password"
      );
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * FROM students");

      while (rs.next()) {
        System.out.println(rs.getInt(1) + " " + rs.getString(2));
      }

      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

This code connects Java to a MySQL database and fetches records from the students table.

यह कोड Java को MySQL database से कनेक्ट करता है और students टेबल से डेटा प्राप्त करता है।

JDBC Code Explanation (Step by Step)

Let's understand each line of the example code:

आइए उदाहरण को लाइन दर लाइन समझें:

Class.forName("com.mysql.cj.jdbc.Driver");

Loads the MySQL JDBC driver so that Java can communicate with the database.

यह MySQL JDBC ड्राइवर को लोड करता है ताकि Java डेटाबेस से कनेक्ट कर सके।

DriverManager.getConnection(...)

Creates a connection to the studentdb MySQL database.

studentdb नामक MySQL डेटाबेस से कनेक्शन बनाता है।

Statement stmt = con.createStatement();

Creates a Statement object to send SQL queries.

Statement ऑब्जेक्ट बनाता है जो SQL queries भेजने के लिए प्रयोग होता है।

ResultSet rs = stmt.executeQuery(...)

Executes the SQL SELECT query and returns a ResultSet with the data.

SQL SELECT query को execute करता है और डेटा के साथ ResultSet लौटाता है।

while (rs.next())

Iterates through the ResultSet to read the data.

ResultSet में डेटा पढ़ने के लिए लूप करता है।

con.close();

Closes the database connection to free resources.

डेटाबेस कनेक्शन को बंद करता है ताकि resources मुक्त हो जाएं।

Summary:
  • Driver Loading: Loads the JDBC driver.
  • Database Connection: Connects to the database.
  • Statement: Prepares SQL queries.
  • Query Execution: Runs SELECT queries and returns results.
  • Result Processing: Reads data from ResultSet.
  • Connection Close: Releases resources.
सारांश:
  • Driver Loading: JDBC ड्राइवर को लोड करता है।
  • Database Connection: डेटाबेस से कनेक्शन बनाता है।
  • Statement: SQL queries तैयार करता है।
  • Query Execution: SELECT queries चलाता है और परिणाम लौटाता है।
  • Result Processing: ResultSet से डेटा पढ़ता है।
  • Connection Close: resources को free करता है।
Expected Output
1 Aryan
2 Riya
3 Amit

This output shows the records fetched from the students table using JDBC.

यह आउटपुट JDBC की मदद से students टेबल से प्राप्त किया गया डेटा दिखाता है।