CodeGym/Java Course/All lectures for TA purposes/தரவுத்தளத்தில் பொருட்களைச் சேமிக்கிறது

தரவுத்தளத்தில் பொருட்களைச் சேமிக்கிறது

கிடைக்கப்பெறுகிறது

JDBC தரவு வகைகளின் முழுமையான பட்டியல்

உங்களுக்குத் தெரிந்த தரவு வகைகளைத் தவிர, DBMSக்கான பல நேட்டிவ் டேட்டா வகைகளுடன் பணிபுரிய JDBC உங்களை அனுமதிக்கிறது. அவற்றைப் பெறுவதற்கான வகைகள் மற்றும் செயல்பாடுகளை கீழே பட்டியலிடுகிறேன்:

வகை முறை
வரிசை getArray()
AsciiStream getAsciiStream()
பிக்டெசிமல் getBigDecimal()
பைனரி ஸ்ட்ரீம் getBinaryStream()
பொட்டு getBlob()
பூலியன் getBoolean()
பொட்டு getBlob()
பூலியன் getBoolean()
பைட்டுகள் getByte()
பைட்டுகள் getBytes()
கேரக்டர் ஸ்ட்ரீம் getCharacterStream()
கட்டை getClob()
தேதி getDate()
இரட்டை getDouble()
மிதவை getFloat()
முழு எண்ணாக getInt()
நீளமானது getLong()
NCharacterStream getNCharacterStream()
பொருள் getObject()
Ref getRef()
RowId getRowId()
குறுகிய getShort()
SQLXML getSQLXML()
லேசான கயிறு getString()
நேரம் getTime()
நேர முத்திரை கெட் டைம்ஸ்டாம்ப்()
யூனிகோட் ஸ்ட்ரீம் getUnicodeStream()
URL getURL()

பழமையான வகைகளை நாங்கள் ஏற்கனவே கருதினோம். இப்போது பொருள்களுடன் வேலை செய்ய முயற்சிப்போம்.

BLOB தரவு வகை

நீங்கள் சில பொருளை தரவுத்தளத்தில் சேமிக்க விரும்பினால், இதைச் செய்வதற்கான எளிதான வழி SQL BLOB வகையைப் பயன்படுத்துவதாகும். ஜே.டி.பி.சி.க்கு ப்ளாப் என்று பெயரிடப்பட்டுள்ளது.

BLOB என்பது பைனரி L arge ஆப்ஜெக்ட்டைக் குறிக்கிறது . பைட்டுகளின் வரிசையை சேமிக்க இது பயன்படுகிறது. ஜேடிபிசியில் உள்ள ப்ளாப் வகை ஒரு இடைமுகம் மற்றும் நீங்கள் அதில் தரவை இரண்டு வழிகளில் வைக்கலாம் (பெறலாம்):

  • இன்புட்ஸ்ட்ரீமைப் பயன்படுத்துதல்
  • பைட்டுகளின் வரிசையைப் பயன்படுத்துதல்

எடுத்துக்காட்டு: நெடுவரிசை எண் 3 BLOB வகையைக் கொண்டுள்ளது:

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

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

உங்கள் சொந்த Blob பொருளை உருவாக்க, நீங்கள் 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;
}

ஜேடிபிசியைப் பயன்படுத்தி இந்த வகுப்பின் ஒரு பொருளை தரவுத்தளத்தில் எவ்வாறு சேமிப்பது?

உண்மையில், நீங்கள் தெரிந்து கொள்ள வேண்டிய அனைத்தையும் நீங்கள் ஏற்கனவே அறிந்திருக்கிறீர்கள். முதலில் நீங்கள் இந்த வகுப்பிற்கு பொருந்தக்கூடிய தரவுத்தளத்தில் ஒரு அட்டவணையை உருவாக்க வேண்டும். உதாரணமாக, இது:

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

மூலம், இதுபோன்ற பல முறைகள் இருந்தால், அவை ஒவ்வொன்றிலும் நீங்கள் ரிசல்ட்செட் சரத்தை ஒரு பொருளாக மாற்ற ஒரே குறியீட்டை எழுத வேண்டும்.பணியாளர். எனவே இந்த குறியீட்டை ஒரு தனி முறைக்கு நகர்த்தலாம்.

பணியாளர் வகுப்பில் Enum, InputStream போன்ற சிக்கலான புலங்கள் அல்லது தரவுத்தளத்தில் நாம் சேமிக்க விரும்பும் பிற பொருட்களைப் பற்றிய குறிப்புகள் இருந்தால் இது மிகவும் பயனுள்ளதாக இருக்கும் .

கருத்துக்கள்
  • பிரபலமானவை
  • புதியவை
  • பழையவை
ஒரு கருத்தைத் தெரிவிக்க நீங்கள் உள்நுழைந்திருக்க வேண்டும்
இந்தப் பக்கத்தில் இதுவரை எந்தக் கருத்தும் வழங்கப்படவில்லை