import java.io.*;
import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;
/**
* <p>名称: BLOBRead.java </p>
* <p>说明:
* 业务规则:
* 读出blob字段
* </p>
* <p>版权: Copyright (c) 2006</p>
* <p>版权所有: 北大青鸟软件工程有限公司</p>
* @author 王世元
* @version 1.0
*/
public class BLOBRead {
public static void main(String[] args) {
Connection con = null;
try {
//连接字串
Class.forName("oracle.jdbc.driver.OracleDriver");
//数据库地址
String url = "jdbc:oracle:thin:@192.168.100.100:1521:wangsy";
//创建连接
con = DriverManager.getConnection(url, "wangsy", "wangsy");
//取消自动提交
con.setAutoCommit(false);
Statement stmt = con.createStatement();
//使用"for update"得到表的写锁
String sql = "select strustring from treestructure where structureid=" +
1;
ResultSet rs = stmt.executeQuery(sql);
BLOB blob = null;
//写出为文件
//File out=new File("c:/outtest1.txt");
//java.io.FileOutputStream fout=new FileOutputStream(out);
//输出字符串
String structureXML = "";
byte[] b = null;
while (rs.next()) {
String tmpStr = "";
blob = (oracle.sql.BLOB) rs.getBlob("strustring");
b = blob.getBytes(1, (int) blob.getLength());
//输入流
InputStream in = blob.getBinaryStream();
DataInputStream dd = new DataInputStream(in);
while ( (tmpStr = dd.readLine()) != null) {
System.out.println("tmpStr " + tmpStr);
structureXML = structureXML + tmpStr;
}
in.close();
dd.close();
}
//关闭连接
con.close();
}
catch (Exception ex) {
try {
con.rollback();
}
catch (SQLException ex1) {
System.out.println(ex1.getMessage());
}
}
}
}