欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java读取blob生成word 博客分类: 技术 oracle 

程序员文章站 2024-03-19 11:03:34
...
1.import java.io.*;  
2.import java.sql.*;  
3. 
4.public class Test {  
5.    Connection con = null;  
6.    Statement stmt = null;  
7.    ResultSet rs = null;  
8. 
9.    private ResultSet getResultSet() {  
10. 
11.        try {  
12.            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
13.            String url = "jdbc:oracle:thin:@10.23.117.110:1521:zgzhms";  
14.            String user = "ibms";  
15.            String password = "ibms";  
16.            con = DriverManager.getConnection(url, user, password);  
17.            stmt = con.createStatement();  
18.            String sql = "SELECT  t.TXN_TRADE FROM  T_TXN_TRADE t   WHERE  t.txn_trade_id = 1";  
19.            rs = stmt.executeQuery(sql);  
20.        } catch (Exception e) {  
21.            e.printStackTrace();  
22.        }  
23.        return rs;  
24.    }  
25. 
26.    public void InputDoc() {  
27.        Test temp = new Test();  
28.        ResultSet rset = temp.getResultSet();  
29.        try {  
30.            while (rset.next()) {  
31.                oracle.sql.BLOB blob = (oracle.sql.BLOB) rset.getBlob("TXN_TRADE");  
32.                File f = new File("C:\\temp.doc");  
33.                FileOutputStream fos = new FileOutputStream(f);  
34.                InputStream is = blob.getBinaryStream();// 读出数据后转换为二进制流  
35.                byte[] data = new byte[1024];  
36.                while (is.read(data) != -1) {  
37.                    fos.write(data);  
38.                }  
39.                fos.close();  
40.                is.close();  
41.            }  
42.            con.commit(); // 正式提交  
43.            rset.close();  
44.        } catch (Exception e) {  
45.        }  
46.    }  
47. 
48.    public static void main(String[] args) {  
49.        Test temp = new Test();  
50.        temp.InputDoc();  
51.    }  
52.} 
相关标签: oracle