1

This is my sample design

d

I have successfully stored a microsoft word document in my sql database (using longblob) using this code below:

Attach button code:

JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
txt_path.setText(filename);

try {
    File doc = new File(filename);
    FileInputStream fis = new FileInputStream(doc);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    for (int readNum; (readNum = fis.read(buf)) != -1;) {
        bos.write(buf, 0, readNum);
    }
    documents = bos.toByteArray();
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

Save button code:

try {
    String sql = "insert into sample (comments, document) values (?,?)";
    pst = conn.prepareStatement(sql);
    pst.setString(1, txt_comments.getText());
    pst.setBytes(2, documents);
    pst.execute();
    updateTable();
    txt_comments.setText("");
    txt_path.setText("");
    JOptionPane.showMessageDialog(null, "Success");
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

Now my problem is I don't know how to open that particular microsoft word document from my sql database by just double clicking the selected column on my jTable.

jeckhart
  • 571
  • 3
  • 10
  • 1
    Save the file into a temp directory and then follow [Using Java, how do I cause Word to open and edit a file?](https://stackoverflow.com/questions/6871664/using-java-how-do-i-cause-word-to-open-and-edit-a-file) – luk2302 Feb 19 '18 at 15:45
  • 1
    Load the file as a blob from the database, save it to a temporary file, then open using `Desktop.getDesktop().open(tempFile);` – TT. Feb 19 '18 at 15:45
  • Okay, I will try it now. Thanks! :) – Feljorim Dumoran Feb 19 '18 at 15:47
  • Wait, how do I load the file as blob from the database and save it to a temporary file? Sorry, I am just a beginner... I only started about 2 weeks ago :-/ – Feljorim Dumoran Feb 19 '18 at 15:50

0 Answers0