CodeGym/Java Course/All lectures for MR purposes/डेटाबेसमध्ये ऑब्जेक्ट्स सेव्ह करणे

डेटाबेसमध्ये ऑब्जेक्ट्स सेव्ह करणे

उपलब्ध

JDBC डेटा प्रकारांची संपूर्ण यादी

तुम्हाला माहीत असलेल्या डेटा प्रकारांव्यतिरिक्त, JDBC तुम्हाला DBMS साठी अनेक मूळ डेटा प्रकारांसह काम करण्याची परवानगी देते. खाली मी ते मिळविण्यासाठी प्रकार आणि कार्ये सूचीबद्ध करेन:

प्रकार पद्धत
रचना getArray()
AsciiStream getAsciiStream()
बिग डेसिमल getBigDecimal()
बायनरीस्ट्रीम getBinaryStream()
ब्लॉब getBlob()
बुलियन getBoolan()
ब्लॉब getBlob()
बुलियन getBoolan()
बाइट्स getByte()
बाइट्स getBytes()
कॅरेक्टरस्ट्रीम GetCaracterStream()
गुंडाळी getClob()
तारीख getDate()
दुहेरी getDouble()
फ्लोट getFloat()
int getInt()
लांब getLong()
एनकॅरॅक्टरस्ट्रीम getNcharacterStream()
ऑब्जेक्ट getObject()
संदर्भ getRef()
RowId getRowId()
लहान getShort()
SQLXML getSQLXML()
स्ट्रिंग getString()
वेळ getTime()
टाईमस्टॅम्प getTimestamp()
युनिकोडस्ट्रीम getUnicodeStream()
URL getURL()

आम्ही आधीच आदिम प्रकारांचा विचार केला आहे. आता ऑब्जेक्ट्ससह कार्य करण्याचा प्रयत्न करूया.

BLOB डेटा प्रकार

तुम्हाला डेटाबेसमध्ये काही ऑब्जेक्ट सेव्ह करायचे असल्यास, SQL BLOB प्रकार वापरणे हा सर्वात सोपा मार्ग आहे. JDBC ला ब्लॉब नावाचा भाग आहे.

BLOB म्हणजे Binary L arge Object . _ हे बाइट्सचे अॅरे संचयित करण्यासाठी वापरले जाते. JDBC मधील ब्लॉब प्रकार हा एक इंटरफेस आहे आणि तुम्ही त्यात दोन प्रकारे डेटा ठेवू शकता (आणि प्राप्त करू शकता):

  • इनपुटस्ट्रीम वापरणे
  • बाइट्सचा अॅरे वापरणे

उदाहरण: स्तंभ क्रमांक 3 मध्ये BLOB प्रकार आहे:

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

डेटासह ब्लॉब पॉप्युलेट करण्याचे दोन मार्ग आहेत. पहिला आउटपुटस्टीम द्वारे आहे :

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

डेटाबेसमध्ये Java ऑब्जेक्ट जतन करणे

डेटाबेसमध्ये बायनरी ऑब्जेक्ट्स कसे सेव्ह करायचे ते आपण शिकलो: बाइट अॅरे, बाइट स्ट्रीम्स इ. जावा ऑब्जेक्ट्सचे काय? डेटाबेसमध्ये Java ऑब्जेक्ट कसे सेव्ह करावे?

समजा आमच्याकडे एम्प्लॉयी जावा क्लास आहे जो कंपनीच्या कर्मचाऱ्याचे वर्णन करतो:

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

साधे आणि स्पष्ट. पद्धत उत्तम चालेल.

डेटाबेसमधून Java ऑब्जेक्ट वाचणे

डेटाबेसवर ऑब्जेक्ट कसा लिहायचा ते आपण शिकलो, आता डेटाबेसमधून ऑब्जेक्ट वाचण्यासाठी कोड लिहू. डेटाबेसमधून ऑब्जेक्ट त्याच्या ID द्वारे वाचणाऱ्या कोडसह प्रारंभ करूया:

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 स्ट्रिंगला ऑब्जेक्टमध्ये रूपांतरित करण्यासाठी समान कोड लिहावा लागेल.कर्मचारी. त्यामुळे हा कोड वेगळ्या पद्धतीने हलवता येतो.

कर्मचारी वर्गात Enum, InputStream सारखी जटिल फील्ड किंवा आम्ही डेटाबेसमध्ये संग्रहित करू इच्छित असलेल्या इतर वस्तूंचे संदर्भ असल्यास हे विशेषतः उपयुक्त ठरू शकते .

टिप्पण्या
  • लोकप्रिय
  • नवीन
  • जुने
टिप्पणी करण्यासाठी तुम्ही साईन इन केलेले असणे आवश्यक आहे
या पानावर अजून कोणत्याही टिप्पण्या नाहीत