2012-10-10 88 views
0

我有一個程序將docx文件存儲到我的MYSQL數據庫MEDIUMBLOB數據類型。從java數據庫讀取docx文件在java中

如何從數據庫中檢索該文件,然後使用APACHE POI讀取它以檢查單詞,段落等的數量? (初始問題)

這是我的代碼保存文件。

public void saveFile(String FileDirectory, String FileName) 
{ 
    try { 
     //Connecting to MYSQL Database 
     Class.forName(driver).newInstance(); 
     con = DriverManager.getConnection(url+dbName,userName,password); 

     //saving the image 
     PreparedStatement psmnt = (PreparedStatement) con.prepareStatement("INSERT INTO doccompfiles VALUES(?,?,?)"); 
     psmnt.setInt(1, 5); 
     psmnt.setNString(2, FileName); 
     File f = new File(FileDirectory + FileName); 
     psmnt.setBlob(3, new FileInputStream(f), f.length()); 
     psmnt.executeUpdate(); 

     con.close(); 
     } 
    catch (Exception e) { 
      JOptionPane.showMessageDialog(null, "Error saving file"); 
      e.printStackTrace(); 
    } 

} 

研究並找到了如何做到這一點,但我在我的堆棧跟蹤有錯誤。這是檢索的代碼。

堆棧跟蹤:

java.sql.SQLException: Before start of result set 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) 
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854) 
    at com.mysql.jdbc.ResultSetImpl.getBinaryStream(ResultSetImpl.java:1553) 
    at com.mysql.jdbc.ResultSetImpl.getBinaryStream(ResultSetImpl.java:1586) 
    at documentComparisor.Database.loadFile(Database.java:150) 
    at documentComparisor.Home$5.actionPerformed(Home.java:195) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

我可以驗證該文件被存儲在數據庫中,並在SELECT語句我的表的詳細信息是正確的。有任何想法嗎?

謝謝!

+0

你可能會忘記了'如果( rs.next())'條件,然後從查詢中檢索流。在你當前的情況下,如果查詢不返回任何內容,你的'rs.getBinaryStream()'可能返回'null'。 –

回答

0
  1. 選擇表 (尋找getBinaryStream在處理大型文件,否則getBlob)

  2. 處理您的BLOB它:

    HWPFDocument doc = new HWPFDocument(binaryStream); 
    WordExtractor we = new WordExtractor(doc); 
    
    String[] paragraphs = we.getParagraphText(); 
    System.out.println("Total Paragraphs: "+paragraphs.length); 
    
    (then iterate through paragraphs array and count words) 
    
+0

您可以請陳述一個關於從「select」語句中檢索BLOB並將其插入到流中的示例嗎?謝謝 – ljpv14

+0

做了一些檢索文件。看到我更新的帖子。 – ljpv14