Welcome to

w3study.github.io




Topics

Database Connection

Example



Database Connection in Advanced Java in Hindi


Database Connection in Hindi

Database Connection का उपयोग Java application और database के बीच communication स्थापित करने के लिए किया जाता है।

Java में database से connect करने के लिए JDBC (Java Database Connectivity) API का उपयोग किया जाता है।

JDBC API के द्वारा SQL queries execute, data retrieve, और update किया जा सकता है।

Java application को database से connect करने के लिए JDBC Driver (जैसे MySQL, Oracle, PostgreSQL, SQL Server) की आवश्यकता होती है।

Database connection के लिए आवश्यक steps:

Connection URL database के प्रकार पर निर्भर करता है:

Database connection secure बनाने के लिए environment variables में credentials store करने चाहिए।

Connection pooling (जैसे HikariCP, Apache DBCP) का उपयोग performance को बेहतर बनाने के लिए किया जाता है।

Web applications में database connection को Servlets, JSP, Hibernate, और Spring Boot के साथ integrate किया जाता है।

Java में database interaction को simplify करने के लिए ORM (Object Relational Mapping) frameworks जैसे Hibernate का उपयोग किया जाता है।

नीचे Java का एक simple JDBC Database Connection Example दिया गया है, जिसमें MySQL database से कनेक्शन बनाया गया है और डेटा को retrieve किया गया है।

Steps:

Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCExample {
    public static void main(String[] args) {
        // Database connection details
        String url = "jdbc:mysql://localhost:3306/testdb"; // Database URL
        String user = "root"; // Database Username
        String password = "password"; // Database Password

        // JDBC Connection, Statement और ResultSet के लिए variables
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // 1. JDBC Driver Load करें
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. Database से कनेक्शन बनाएं
            con = DriverManager.getConnection(url, user, password);
            System.out.println("Database connected successfully!");

            // 3. SQL Query तैयार करें और execute करें
            stmt = con.createStatement();
            String query = "SELECT id, name, email FROM users";
            rs = stmt.executeQuery(query);

            // 4. ResultSet से डेटा प्राप्त करें
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");

                System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("JDBC Driver not found!");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Database connection error!");
            e.printStackTrace();
        } finally {
            // 5. Resources को close करें
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (con != null) con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

  • JDBC Statement in Hindi

  • Advanced Java Notes in Hindi

  • Development of Android Applications Notes in Hindi


  • Request:

    हैलो दोस्तों! उम्मीद करता हूं आपको हमारा यह content/post पसंद आया होगा। अगर आपको हमारा ये content/post पसंद आई हो तो अपने दोस्तों के पास भी share करे। और अगर आपको कोई problem या कोई specific content हिन्दी में चाहिए है तो आप हमें नीचे दिए गए Email या whatsapp number के जरिए बता सकते है।

    अगर आप CCC/diploma/polytechnic/MCA/BCA etc कर रहे है तो ये website स्पेशली आपके लिए ही है, जो student हिंदी में पढ़ाई करते है।

    Contact Us

    Email: deepanshuranjan8057@gmail.com

    Whatsapp: +91 8057754706



    Follow Us

    Facebook Logo    Instagram Logo