CodeGym/Java Course/All lectures for HI purposes/वस्तुओं को डेटाबेस में सहेजना

वस्तुओं को डेटाबेस में सहेजना

उपलब्ध

JDBC डेटा प्रकारों की पूरी सूची

आपके द्वारा ज्ञात डेटा प्रकारों के अलावा, JDBC आपको DBMS के लिए कई मूल डेटा प्रकारों के साथ काम करने की अनुमति देता है। नीचे मैं उन्हें प्राप्त करने के लिए प्रकार और कार्यों की सूची दूंगा:

प्रकार तरीका
सरणी गेटअरे ()
असीसीस्ट्रीम getAsciiStream ()
बिगडेसिमल getBigDecimal ()
बाइनरीस्ट्रीम गेटबाइनरीस्ट्रीम ()
ब्लॉब गेटब्लॉब ()
बूलियन गेटबूलियन ()
ब्लॉब गेटब्लॉब ()
बूलियन गेटबूलियन ()
बाइट्स गेटबाइट ()
बाइट्स गेटबाइट्स ()
कैरेक्टरस्ट्रीम getCharacterStream ()
clob गेटक्लोब ()
तारीख तारीख लें()
दोहरा डबल प्राप्त करें ()
तैरना गेटफ्लोट ()
int यहाँ getInt ()
लंबा लंबा समय लें()
एनकैरेक्टरस्ट्रीम getNCharacterStream ()
वस्तु गेटऑब्जेक्ट ()
संदर्भ getRef ()
RowId getRowId ()
छोटा गेटशॉर्ट ()
SQLXML getSQLXML()
डोरी गेटस्ट्रिंग ()
समय समय निकालो()
समय-चिह्न गेटटाइमस्टैम्प ()
यूनिकोडस्ट्रीम यूनिकोडस्ट्रीम प्राप्त करें ()
यूआरएल गेटयूआरएल ()

हम पहले ही आदिम प्रकारों पर विचार कर चुके हैं। आइए अब वस्तुओं के साथ काम करने का प्रयास करें।

ब्लॉब डेटा प्रकार

यदि आप किसी वस्तु को डेटाबेस में सहेजना चाहते हैं, तो ऐसा करने का सबसे आसान तरीका SQL BLOB प्रकार का उपयोग करना है। JDBC का समकक्ष ब्लॉब है।

बीएलओबी का मतलब बाइनरी एल आर्गे ऑब्जेक्ट हैइसका उपयोग बाइट्स की एक सरणी को स्टोर करने के लिए किया जाता है। JDBC में Blob प्रकार एक इंटरफ़ेस है और आप इसमें दो तरह से डेटा डाल सकते हैं (और प्राप्त कर सकते हैं):

  • इनपुटस्ट्रीम का उपयोग करना
  • बाइट्स की एक सरणी का उपयोग करना

उदाहरण: स्तंभ संख्या 3 में ब्लॉब प्रकार होता है:

Statement statement = connection.createStatement();
    ResultSet results = statement.executeQuery("SELECT * FROM user");
    results.first();

    Blob blob = results.getBlob(3);
    InputStream is = blob.getBinaryStream();

अपना स्वयं का ब्लॉब ऑब्जेक्ट बनाने के लिए, आपको createBlob() फ़ंक्शन का उपयोग करने की आवश्यकता है । उदाहरण:

String insertQuery = “INSERT INTO images(name, image) VALUES (?, ?);
PreparedStatement statement = connection.prepareStatement(insertQuery);

// Create a Blob object and get an OtputStream from it to write data to it
Blob blob = connection.createBlob();

// Populate the Blob with data...
OutputStream os = blob.setBinaryStream(1);

// Passing Blob as query parameter
statement.setBlob(2, blob);
statement.execute();

डेटा के साथ ब्लॉब को आबाद करने के दो तरीके हैं। पहले वाला OutputSteam के माध्यम से है :

Path avatar = Paths.get("E:\\images\\cat.jpg");
OutputStream os = blob.setBinaryStream(1);
Files.copy(avatar, os);

और दूसरा - बाइट्स भरकर:

Path avatar = Paths.get("E:\\images\\cat.jpg");
byte[] content = Files.readAllBytes(avatar);
blob.setBytes(1, content);

जावा ऑब्जेक्ट को डेटाबेस में सहेजना

हमने सीखा है कि बाइनरी ऑब्जेक्ट्स को डेटाबेस में कैसे सहेजना है: बाइट एरे, बाइट स्ट्रीम, और इसी तरह। जावा ऑब्जेक्ट्स के बारे में क्या? हम जावा ऑब्जेक्ट को डेटाबेस में कैसे सहेज सकते हैं?

मान लीजिए कि हमारे पास एक कर्मचारी जावा वर्ग है जो किसी कंपनी के कर्मचारी का वर्णन करता है:

public class Employee {
    public Integer id;
    public String name;
    public String occupation;
    public Integer salary;
    public Date joinDate;
}

हम JDBC का उपयोग करके इस वर्ग की किसी वस्तु को डेटाबेस में कैसे सहेज सकते हैं?

वास्तव में, आप पहले से ही वह सब कुछ जानते हैं जो आपको जानने की आवश्यकता है। सबसे पहले आपको इस वर्ग से संबंधित डेटाबेस में एक तालिका बनाने की आवश्यकता है। उदाहरण के लिए, यह एक:

CREATE TABLE employee {
    id INT PRIMARY KEY NOT NULL,
    name VARCHAR(100),
    occupation VARCHAR(100),
    salary INT,
    join_date DATE
}

और अब कोड लिखते हैं जो हमारे कर्मचारी वर्ग के एक ऑब्जेक्ट को आधार से जोड़ देगा:

public static boolean addEmployee(Connection connection, Employee employee) throws Exception {
    	// Create and prepare a query to insert data into the table
    	String insertQuery = "INSERT INTO employee(name, occupation, salary, join_date ) VALUES (?, ?, ?, ?)";
    	PreparedStatement statement = connection.prepareStatement(insertQuery);

    	// Populate the query with data from the Employee object
    	statement.setString(1, employee.name);
    	statement.setString(2, employee.occupation);
    	statement.setInt(3, employee.salary);
    	statement.setDate(4, employee.joinDate);

    	// Execute our query and it returns true if a new row has been added
    	int count = statement.executeUpdate();
    	return count > 0;
    }

सरल और स्पष्ट। तरीका बहुत अच्छा काम करेगा।

डेटाबेस से जावा ऑब्जेक्ट पढ़ना

हमने डेटाबेस में ऑब्जेक्ट लिखना सीखा, अब डेटाबेस से ऑब्जेक्ट को पढ़ने के लिए कोड लिखते हैं। आइए उस कोड से शुरू करें जो डेटाबेस से किसी ऑब्जेक्ट को उसकी आईडी से पढ़ता है:

public static Employee getEployeeById(Connection connection, int id) throws Exception {
    	// Create and prepare a query to get an employee from the table
    	PreparedStatement statement = connection.prepareStatement("SELECT * FROM employee WHERE id = ?");
    	statement.setInt(1, id);

    	// Execute our query and it returns null if there are no rows in the query result
    	ResultSet results = statement.executeQuery();
    	if (!results.first())
        	return null;

    	// Populate the Employee object with the data from the ResultSet
    	Employee employee = new Employee();
    	employee.id = results.getInt(1);
    	employee.name = results.getString(2);
    	employee.occupation = results.getString(3);
    	employee.salary = results.getInt(4);
    	employee.joinDate = results.getDate(5);
    	return employee;
}

और अगर हमें एक वस्तु की नहीं, बल्कि कई की आवश्यकता है? ऐसा अनुरोध लिखना भी आसान है। आइए हमारी कंपनी के सभी कर्मचारियों को प्राप्त करें:

public static List<Employee> getAllEployees(Connection connection) throws Exception {
	// Create and execute a query to get employees from the table
	Statement statement = connection.createStatement();
	ResultSet results = statement.executeQuery("SELECT * FROM employee");

	ArrayList<Employee> list = new ArrayList<Employee>();
	while (results.next()) {
        // Populate the Employee object with the data from the current row of the ResultSet
	        Employee employee = new Employee();
	        employee.id = results.getInt(1);
	        employee.name = results.getString(2);
	        employee.occupation = results.getString(3);
	        employee.salary = results.getInt(4);
	        employee.joinDate = results.getDate(5);

	        list.add(employee);
	}
	return list;
}

वैसे, यदि ऐसी कई विधियाँ हैं, तो उनमें से प्रत्येक में आपको ResultSet स्ट्रिंग को एक ऑब्जेक्ट में बदलने के लिए एक ही कोड लिखना होगा।कर्मचारी. तो इस कोड को एक अलग विधि में ले जाया जा सकता है।

यह विशेष रूप से उपयोगी हो सकता है यदि कर्मचारी वर्ग में एनम, इनपुटस्ट्रीम, या अन्य वस्तुओं के संदर्भ जैसे जटिल क्षेत्र शामिल हैं जिन्हें हम डेटाबेस में भी स्टोर करना चाहते हैं।

टिप्पणियां
  • लोकप्रिय
  • नया
  • पुराना
टिप्पणी लिखने के लिए आपको साइन इन करना होगा
इस पेज पर अभी तक कोई टिप्पणियां नहीं हैं