จัดลำดับและทำงานกับเอกสารในฐานข้อมูล
หนึ่งในงานที่คุณอาจต้องดำเนินการเมื่อทำงานกับเอกสารคือการจัดเก็บและดึงออบเจกต์Documentไปและกลับจากฐานข้อมูล งเช่นนี้จะมีความจำเป็นถ้าคุณกำลังใช้ระบบการจัดการเนื้อหาชนิดใดๆ เอกสารรุ่นก่อนหน้าทั้งหมดจะต้องถูกเก็บไว้ในระบบฐานข้อมูล ความสามารถในการจัดเก็บเอกสารในฐานข้อมูลยังมีประโยชน์อย่างมากเมื่อโปรแกรมป.
Aspose.Wordsให้ความสามารถในการแปลงเอกสารเป็นอาร์เรย์ไบต์สำหรับงานที่ตามมากับเอกสารนี้.
แปลงเอกสารเป็นอาร์เรย์ไบต์
การจัดเก็บเอกสารในฐานข้อมูลหรือการเตรียมเอกสารสำหรับการส่งผ่านเว็บก็มักจะจำ.
เพื่อจัดลำดับวัตถุDocumentในAspose.Words:
- บันทึกลงในMemoryStreamโดยใช้Saveวิธีการโอเวอร์โหลดของDocumentคลาส.
- เรียกวิธีการToArrayซึ่งส่งกลับอาร์เรย์ของไบต์ที่แสดงเอกสารในรูปแบบไบต์.
ขั้นตอนข้างต้นสามารถย้อนกลับเพื่อโหลดไบต์กลับไปยังวัตถุDocument.
ตัวอย่างด้านล่างแสดงวิธีการจัดลำดับวัตถุDocumentเพื่อขอรับอาร์เรย์ไบต์และจากนั้นวิธีการยกเลิกการเรียงลำดับอาร์เรย์ไบต์เพื่อขอรับวัตถุDocumentอีกครั้ง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the document. | |
Document doc = new Document(dataDir + "Test File (doc).doc"); | |
// Create a new memory stream. | |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | |
// Save the document to stream. | |
doc.save(outStream, SaveFormat.DOCX); | |
// Convert the document to byte form. | |
byte[] docBytes = outStream.toByteArray(); | |
// The bytes are now ready to be stored/transmitted. | |
// Now reverse the steps to load the bytes back into a document object. | |
ByteArrayInputStream inStream = new ByteArrayInputStream(docBytes); | |
// Load the stream into a new document object. | |
Document loadDoc = new Document(inStream); |
เก็บอ่านและลบเอกสารในฐานข้อมูล
ส่วนนี้แสดงวิธีการบันทึกเอกสารในฐานข้อมูลและโหลดกลับไปยังออบเจกต์Document
สำหรับการ เพื่อความเรียบง่ายชื่อแฟ้มเป็นคีย์ที่ใช้ในการจัดเก็บและดึงเอกสารจากฐานข้อมูล ฐานข้อมูลประกอบด้วยสองคอลัมน์ คอลัมน์แรก"FileName"จะถูกเก็บเป็นสตริงและใช้เพื่อระบุเอกสาร คอลัมน์ที่สอง"FileContent"ถูกเก็บไว้เป็นวัตถุBLOB
ซึ่งจัดเก็บวัตถุเอกสารในรูปแบบไบต์.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าการเชื่อมต่อกับฐานข้อมูลและรันคำสั่ง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String url1 = "jdbc:mysql://localhost:3306/test"; | |
String user = "root"; | |
String password = "123"; | |
// Open a database connection. | |
Connection mConnection = DriverManager.getConnection(url1, user, password); |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการบันทึกเอกสารไปยังฐานข้อมูลแล้วอ่านเอกสารเดียวกันอี:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Store the document to the database. | |
StoreToDatabase(doc, mConnection); | |
// Read the document from the database and store the file to disk. | |
Document dbDoc = ReadFromDatabase(dataDir + fileName, mConnection); | |
// Save the retrieved document to disk. | |
dbDoc.save(dataDir + fileName); | |
// Delete the document from the database. | |
DeleteFromDatabase(dataDir + fileName, mConnection); | |
// Close the connection to the database. | |
mConnection.close(); |
บันทึกเอกสารไปยังฐานข้อมูล
เมื่อต้องการบันทึกเอกสารในฐานข้อมูลแปลงเอกสารนี้เป็นอาร์เรย์ของไบต์ตามที่อธิบาย จากนั้นบันทึกอาร์เรย์ไบต์นี้ลงในฟิลด์ฐานข้อมูล.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการบันทึกเอกสารไปยังฐานข้อมูลที่ระบุ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void StoreToDatabase(Document doc, Connection mConnection) throws Exception { | |
// Create an output stream which uses byte array to save data | |
ByteArrayOutputStream aout = new ByteArrayOutputStream(); | |
// Save the document to byte array | |
doc.save(aout, SaveFormat.DOCX); | |
// Get the byte array from output steam | |
// the byte array now contains the document | |
byte[] buffer = aout.toByteArray(); | |
// Get the filename from the document. | |
String fileName = doc.getOriginalFileName(); | |
String filePath = fileName.replace("\\", "\\\\"); | |
// Create the SQL command. | |
String commandString = "INSERT INTO Documents (FileName, FileContent) VALUES('" + filePath + "', '" + buffer | |
+ "')"; | |
Statement statement = mConnection.createStatement(); | |
statement.executeUpdate(commandString); | |
} |
ระบุcommandStringซึ่งเป็นSQLนิพจน์ที่ทำงานทั้งหมด:
- เมื่อต้องการบันทึกเอกสารลงในฐานข้อมูลใช้คำสั่ง"INSERTINTO"และตารางที่ระบุพร้อมกับค่าของสองเขตข้อมูลระเบียน-FileNameและFileContent เมื่อต้องการหลีกเลี่ยงพารามิเตอร์เพิ่มเติมชื่อไฟล์จะถูกนำมาจากออบเจกต์Documentตัวเอง ค่าฟิลด์
FileContent
ถูกกำหนดไบต์จากสตรีมหน่วยความจำซึ่งประกอบด้วยตัวแทนไบนารีของเอกสารที่. - บรรทัดที่เหลือของรหัสรันคำสั่งที่เก็บเอกสารAspose.Wordsในฐานข้อมูล.
ดึงเอกสารจากฐานข้อมูล
เมื่อต้องการดึงเอกสารจากฐานข้อมูลให้เลือกระเบียนที่ประกอบด้วยข้อมูลเอกสารเป็นอ แล้วโหลดอาร์เรย์ไบต์จากเรกคอร์ดลงในMemoryStreamและสร้างวัตถุDocumentที่จะโหลดเอกสารจากMemoryStream.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงข้อมูลและส่งคืนเอกสารจากฐานข้อมูลที่ระบุโดยใช้ชื่อ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static Document ReadFromDatabase(String fileName, Connection mConnection) throws Exception { | |
// Create the SQL command. | |
String commandString = "SELECT * FROM Documents WHERE FileName=?"; | |
PreparedStatement statement = mConnection.prepareStatement(commandString); | |
statement.setString(1, fileName); | |
Document doc = null; | |
ResultSet result = statement.executeQuery(); | |
if (result.next()) { | |
Blob blob = result.getBlob("FileContent"); | |
InputStream inputStream = blob.getBinaryStream(); | |
doc = new Document(inputStream); | |
inputStream.close(); | |
System.out.println("File saved"); | |
} | |
result.close(); | |
return doc; | |
} |
ลบเอกสารจากฐานข้อมูล
เมื่อต้องการลบเอกสารออกจากฐานข้อมูลให้ใช้คำสั่งSQLที่เหมาะสมโดยไม่มีการจัดกิจวัตรใดๆบนวัตถุDocument.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบเอกสารจากฐานข้อมูลโดยใช้ชื่อแฟ้มเพื่อดึงข้อมูลเรก:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static void DeleteFromDatabase(String fileName, Connection mConnection) throws Exception { | |
// Create the SQL command. | |
String commandString = "DELETE FROM Documents WHERE FileName='" + fileName + "'"; | |
Statement statement = mConnection.createStatement(); | |
// Delete the record. | |
statement.execute(commandString); | |
} |