JDBC in Java with Example (CRUD Operations for Beginners)

JDBC in Java with Example (CRUD Operations for Beginners)

If you want to build real-world Java applications, learning JDBC is essential.

When I first learned Java, I understood basic concepts but didn’t know how to connect my program to a database. That’s where JDBC helped me.

Written by Shivkumar Udas – Engineering student and AWS Cloud Club core member.


💡 What is JDBC?

JDBC (Java Database Connectivity) is an API that allows Java programs to connect with databases and perform operations like insert, update, delete, and retrieve data.


📊 How JDBC Works (Simple Architecture)

JDBC works as a bridge between your Java application and the database:

  • Java program sends request
  • JDBC driver connects to database
  • SQL query is executed
  • Result is returned to Java program

📌 Why JDBC is Important

  • Used in real-world applications
  • Connects Java with databases
  • Required for backend development

📌 Steps to Use JDBC

  1. Load the driver
  2. Establish connection
  3. Create statement
  4. Execute query
  5. Close connection

📌 Example: Connecting Java to Database


import java.sql.Connection;
import java.sql.DriverManager;

public class TestDB {
    public static void main(String[] args) {
        try {
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/testdb",
                "root",
                "password"
            );

            System.out.println("Connected successfully");

        } catch(Exception e){
            System.out.println(e);
        }
    }
}

📌 CRUD Operations in JDBC

Create (Insert)


String query = "INSERT INTO students VALUES(1, 'Shiv')";

Read (Select)


String query = "SELECT * FROM students";

Update


String query = "UPDATE students SET name='Ram' WHERE id=1";

Delete


String query = "DELETE FROM students WHERE id=1";

📌 Using PreparedStatement (Recommended)


import java.sql.PreparedStatement;

String query = "INSERT INTO students(name) VALUES(?)";
PreparedStatement ps = con.prepareStatement(query);

ps.setString(1, "Shiv");
ps.executeUpdate();

PreparedStatement is safer because it prevents SQL injection and improves performance.


📌 Important JDBC Classes

  • DriverManager – Manages database connection
  • Connection – Represents connection
  • Statement – Executes SQL queries
  • PreparedStatement – Secure query execution
  • ResultSet – Stores query results

🔍 Real-Life Use Case

JDBC is used in applications like login systems, where user data is stored and retrieved from a database.


⚠️ Common Mistakes to Avoid

  • Wrong database URL
  • Forgetting to close connection
  • Using Statement instead of PreparedStatement

🧪 Practice Task

Create a database table and write a Java program to insert and display records.


📈 Next Step in Learning

After JDBC, learn frameworks like Spring Boot to build real-world applications.


🎯 Conclusion

JDBC is an essential concept for Java developers. It allows your applications to interact with databases and build real-world systems.

Practice CRUD operations regularly to gain confidence.

Post a Comment

Previous Post Next Post