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.

Comments