deepeweb.github.io

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 करने चाहिए।

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

Steps:

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();
            }
        }
    }
}



Request

अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!

ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।

आपके support और understanding के लिए दिल से धन्यवाद।




Follow Us

Facebook Logo    Instagram Logo