Skip to content

13.1 JDBC Fundamentals & CRUD

⚙️ 5 Core Steps of JDBC

import java.sql.*;

public class JdbcCrudDemo {
    private static final String URL = "jdbc:postgresql://localhost:5432/fincz_db";
    private static final String USER = "postgres";
    private static final String PASS = "secret";

    public static void main(String[] args) {
        String insertSql = "INSERT INTO users (name, email) VALUES (?, ?)";

        // Try-with-resources से Connection और PreparedStatement ऑटोमैटिक क्लोज होंगे
        try (Connection conn = DriverManager.getConnection(URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(insertSql)) {

            // पैरामीटर्स सेट करें (100% SQL Injection Safe)
            pstmt.setString(1, "Ahmad");
            pstmt.setString(2, "[email protected]");

            int rows = pstmt.executeUpdate();
            System.out.println("Inserted Rows: " + rows);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

🧭 Navigation

Last updated on