1

docxファイルをMYSQLデータベースのMEDIUMBLOBデータ型に保存するプログラムがあります。

単語や段落などの数を確認するために、データベースからそのファイルを取得し、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();
     }

}

それを行う方法を調査して見つけましたが、スタックトレースでエラーが発生しています。取得するためのコードは次のとおりです。

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

        Statement stmt = (Statement) con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM doccompfiles WHERE FileName = '"+ FileName +"'");
        InputStream is = rs.getBinaryStream("File");

        HWPFDocument doc = new HWPFDocument(is);
        WordExtractor we = new WordExtractor(doc);

        String[] paragraphs = we.getParagraphText();
        JOptionPane.showMessageDialog(null, "Number of Paragraphs" + paragraphs.length);
        con.close();
    }
    catch(Exception ex)
    {
        ex.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ステートメントのテーブルの詳細が正しいことを確認できます。何か案は?

ありがとう!

4

2 に答える 2

1
  1. テーブルから BLOB を選択します (大きなファイルの処理中は getBinaryStream を探します。それ以外の場合は getBlob を探します)。

  2. それを処理します:

    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)
    
于 2012-10-10T20:12:55.760 に答える