CRUD Operations
CRUD stands for Create, Read, Update, and Delete — the four basic operations performed on data stored in a database, corresponding to SQL commands INSERT, SELECT, UPDATE, and DELETE.
1. What are CRUD Operations?
CRUD stands for Create, Read, Update, and Delete — the four basic operations performed on data stored in a database, corresponding to SQL commands INSERT, SELECT, UPDATE, and DELETE.
2. Why is it used?
Almost every application that uses a database needs to perform these four fundamental actions on its data — adding new records, viewing existing ones, modifying them, and removing them when no longer needed.
3. Real-Life Example
Think of managing a library's book records: adding a new book (Create), searching for a book (Read), updating a book's availability status (Update), and removing a book that's no longer part of the collection (Delete).
4. Syntax
java// Create "INSERT INTO table (col1, col2) VALUES (?, ?)" // Read "SELECT * FROM table WHERE condition" // Update "UPDATE table SET col1 = ? WHERE condition" // Delete "DELETE FROM table WHERE condition"
5. Example Program
javaimport java.sql.*; public class CrudDemo { public static void main(String[] args) throws SQLException { Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/schooldb", "root", "password" ); PreparedStatement insert = conn.prepareStatement( "INSERT INTO students (name, age) VALUES (?, ?)" ); insert.setString(1, "Kabir"); insert.setInt(2, 20); insert.executeUpdate(); System.out.println("Student record created successfully."); conn.close(); } }
Output:
Student record created successfully.6. Key Points to Remember
executeUpdate()is used forINSERT,UPDATE, andDELETE;executeQuery()is used specifically forSELECT.- CRUD forms the foundation of almost every data-driven application, from simple apps to large enterprise systems.
- Using
PreparedStatementfor all CRUD operations is the safer, recommended practice.