JAVAはblobデータを操作してwordファイルを生成する

1647 ワード

import java.io.*;
import java.sql.*;

public class Test {
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;

	private ResultSet getResultSet() {

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url = "jdbc:oracle:thin:@10.23.117.110:1521:zgzhms";
			String user = "ibms";
			String password = "ibms";
			con = DriverManager.getConnection(url, user, password);
			stmt = con.createStatement();
			String sql = "SELECT  t.TXN_TRADE FROM  T_TXN_TRADE t	WHERE  t.txn_trade_id = 1";
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	public void InputDoc() {
		Test temp = new Test();
		ResultSet rset = temp.getResultSet();
		try {
			while (rset.next()) {
				oracle.sql.BLOB blob = (oracle.sql.BLOB) rset.getBlob("TXN_TRADE");
				File f = new File("C:\\temp.doc");
				FileOutputStream fos = new FileOutputStream(f);
				InputStream is = blob.getBinaryStream();//             
				byte[] data = new byte[1024];
				while (is.read(data) != -1) {
					fos.write(data);
				}
				fos.close();
				is.close();
			}
			con.commit(); //     
			rset.close();
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) {
		Test temp = new Test();
		temp.InputDoc();
	}
}