Serialize ולעבוד עם מסמך במסד נתונים

אחת המשימות שאתה יכול לעשות כאשר עבודה עם מסמכים היא אחסון ושיקום Document אובייקטים וממסד נתונים. לדוגמה, זה יהיה הכרחי אם תיישם כל סוג של מערכת ניהול תוכן. יש לאחסן את כל הגרסאות הקודמות של המסמכים במערכת מסד הנתונים. היכולת לאחסן מסמכים במסד הנתונים היא גם שימושית ביותר כאשר היישום שלך מספק שירות מבוסס אינטרנט.

Aspose.Words מספק יכולת להמיר מסמך לתוך מערך עוטה עבור עבודה לאחר מכן עם מסמך זה במסד נתונים.

עקבו אחרי Byte Array

כדי לאחסן מסמך במסד נתונים או להכין מסמך לשידור ברחבי האינטרנט, לעתים קרובות יש צורך לקבוע את המסמך כדי להשיג מערך עוטה.

כדי לנסח Document אובייקט Aspose.Words:

1.1 1. להציל אותו ל MemoryStream באמצעות Save המונחים: overload Document מעמד. 1.1 1. התקשר ToArray שיטה, אשר מחזירה מערך של עיתות המייצגות את המסמך בצורה עוטה.

השלבים לעיל ניתן להפוך כדי לטעון את המטות בחזרה לתוך a Document אובייקט

הדוגמה הבאה מראה כיצד לארגן Document להתנגד להשגת מערך עוטה, ולאחר מכן כיצד להשבית את מערך ה- Byte כדי להשיג מערך 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 להתנגד לעבוד איתו. לשם פשטות, שם הקובץ הוא המפתח המשמש לאחסון ולמשוך מסמכים ממסד הנתונים. מסד הנתונים מכיל שני עמודים. העמודה הראשונה “File Name” מאוחסנת כ String ומשמשת לזיהוי מסמכים. העמודה השנייה “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);
}

הוראות ספציפיות, שהיא ביטוי של SQL שעושה את כל העבודה:

    • כדי לשמור מסמך לתוך מסד הנתונים, הפקודה “INSERT INTO” משמשת ושולחן שצוין יחד עם הערכים של שני שדות שיא - שם קובץ ו- Filecontent. כדי למנוע פרמטרים נוספים, שם הקובץ נלקח מן Document להתנגד לעצמו. The The The 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);
}