కోడ్‌జిమ్/జావా కోర్సు/All lectures for TE purposes/డేటాబేస్‌లో వస్తువులను సేవ్ చేస్తోంది

డేటాబేస్‌లో వస్తువులను సేవ్ చేస్తోంది

అందుబాటులో ఉంది

JDBC డేటా రకాల పూర్తి జాబితా

మీకు తెలిసిన డేటా రకాలతో పాటు, DBMS కోసం అనేక స్థానిక డేటా రకాలతో పని చేయడానికి JDBC మిమ్మల్ని అనుమతిస్తుంది. క్రింద నేను వాటిని పొందడానికి రకాలు మరియు విధులను జాబితా చేస్తాను:

టైప్ చేయండి పద్ధతి
అమరిక getArray()
AsciiStream getAsciiStream()
బిగ్ డెసిమల్ getBigDecimal()
బైనరీ స్ట్రీమ్ getBinaryStream()
బొట్టు getBlob()
బూలియన్ getBoolean()
బొట్టు getBlob()
బూలియన్ getBoolean()
బైట్లు getByte()
బైట్లు getBytes()
క్యారెక్టర్ స్ట్రీమ్ getCharacterStream()
బొట్టు getClob()
తేదీ getDate()
రెట్టింపు getDouble()
తేలుతుంది getFloat()
int getInt()
పొడవు getLong()
NCharacterStream getNCharacterStream()
వస్తువు getObject()
Ref getRef()
RowId getRowId()
చిన్నది getShort()
SQLXML getSQLXML()
స్ట్రింగ్ getString()
సమయం getTime()
సమయముద్ర getTimestamp()
యూనికోడ్ స్ట్రీమ్ getUnicodeStream()
URL getURL()

మేము ఇప్పటికే ఆదిమ రకాలను పరిగణించాము. ఇప్పుడు వస్తువులతో పని చేయడానికి ప్రయత్నిద్దాం.

BLOB డేటా రకం

మీరు డేటాబేస్‌లో కొంత వస్తువును సేవ్ చేయాలనుకుంటే, దీన్ని చేయడానికి సులభమైన మార్గం SQL BLOB రకాన్ని ఉపయోగించడం. JDBC దాని ప్రతిరూపాన్ని Blob అని పిలుస్తారు.

BLOB అంటే బైనరీ ఎల్ ఆర్జ్ ఆబ్జెక్ట్ . _ ఇది బైట్‌ల శ్రేణిని నిల్వ చేయడానికి ఉపయోగించబడుతుంది. 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();

డేటాతో బొట్టును నింపడానికి రెండు మార్గాలు ఉన్నాయి. మొదటిది 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;
    }

సాధారణ మరియు స్పష్టమైన. పద్ధతి గొప్పగా పని చేస్తుంది.

డేటాబేస్ నుండి జావా వస్తువును చదవడం

డేటాబేస్‌కు ఆబ్జెక్ట్‌ను ఎలా వ్రాయాలో నేర్చుకున్నాము, ఇప్పుడు డేటాబేస్ నుండి ఆబ్జెక్ట్‌ను చదవడానికి కోడ్‌ను వ్రాద్దాం. డేటాబేస్ నుండి ఒక వస్తువును దాని 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;
}

మార్గం ద్వారా, అటువంటి అనేక పద్ధతులు ఉంటే, వాటిలో ప్రతి దానిలో మీరు రిజల్ట్‌సెట్ స్ట్రింగ్‌ను ఆబ్జెక్ట్‌గా మార్చడానికి ఒకే కోడ్‌ను వ్రాయవలసి ఉంటుంది.ఉద్యోగి. కాబట్టి ఈ కోడ్‌ని ప్రత్యేక పద్ధతికి తరలించవచ్చు.

ఎంప్లాయీ క్లాస్‌లో ఎనమ్, ఇన్‌పుట్‌స్ట్రీమ్ వంటి సంక్లిష్ట ఫీల్డ్‌లు లేదా మేము డేటాబేస్‌లో నిల్వ చేయాలనుకుంటున్న ఇతర ఆబ్జెక్ట్‌ల రిఫరెన్స్‌లు ఉంటే ఇది ప్రత్యేకంగా ఉపయోగపడుతుంది .

వ్యాఖ్యలు
  • జనాదరణ పొందినది
  • కొత్తది
  • పాతది
వ్యాఖ్యానించడానికి మీరు తప్పనిసరిగా సైన్ ఇన్ చేసి ఉండాలి
ఈ పేజీకి ఇంకా ఎలాంటి వ్యాఖ్యలు లేవు