BLOB是特殊的类型,需要自己重新写代码读的。不能用缺省的STRING读法。
解决方案 »
- new String hashSet
- 类定义的一道选择题
- 非jsp关于关键字高亮问题
- 如何在j2sdk SE v1.4.2下配置Servlet?
- 求两个日期之间的天数时出了错
- 求一个函数....
- 初等几何-椭圆滑动 http://steiner.math.nthu.edu.tw/ne01/tjy/edu-ellipse/slide-ellipse.htm
- 这个问题怎么解决人?
- 帮帮忙:怎样将一个IP地址转换成一个二进制的数字(32位)
- 大家说Java Network Programming这本书怎么样?
- 我想把比如"http://www.sina.com/aa.doc"中的aa.doc文件下载到本地机子并存到本地,(将服务器指定的文件下载到客户端并自动保存到指定的
- 高分相赠!!!!!!!!!!无法读取blob数据,急!!!!!!!!!!!!!!!!!!!!!!!!
sbuffer.append(cbuffer,0,index); } newRow.addElement(sbuffer);以前写的,反正能用。看看吧
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.sql.BLOB;
import oracle.sql.*;public class LOBUtil {
public LOBUtil(){
} /**
* String2LOB store String to LOB
* @param data String need to be stored.
* @param table_name Table name
* @param lob_field_name LOB Field
* @param key_field Primary Key Field
* @param key_value Primary Key value
*/
public boolean String2LOB(Connection conn,
String data,
String table_name,
String lob_field_name,
String key_field,
String key_value)throws DBAccessException{
try {
boolean commit=conn.getAutoCommit();
BLOB lob = (BLOB)getBLOB(conn,table_name,lob_field_name,key_field,key_value);
OutputStream os = lob.getBinaryOutputStream();
java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(os);
bos.write(data.getBytes());//Stored String data to BLOB field
bos.flush();
bos.close();
os.close();
conn.commit();
conn.setAutoCommit(commit);
return true;
} catch(SQLException sqle){
throw new DBAccessException(sqle);
}catch (Exception err) {
throw new DBAccessException(err);
}
} private static void log(String msg){
System.out.println(msg);
} private Blob getBLOB(Connection conn,
String table_name,
String lob_field_name,
String key_field,
String key_value)throws DBAccessException{
Blob lob = null;
try {
String sql = "select " + lob_field_name + " from " + table_name + " where " + key_field + " = '" + key_value + "' for update ";
conn.setAutoCommit(false);
Statement pstmt = conn.createStatement();
java.sql.ResultSet rs = pstmt.executeQuery(sql);
if (rs.next()) {
//lob = ((OracleResultSet)rs).getBLOB(1);
lob = rs.getBlob(1);
return lob;
} else {
pstmt.close();
throw new DBAccessException("LOB field can not be found. Please check it again.");
} } catch (Exception err) {
try{
conn.rollback();
}catch(SQLException sqle){
throw new DBAccessException(sqle);
}
throw new DBAccessException(err.getMessage());
} } /**
* LOB2String get String data from LOB
* @param conn @see java.sql.Connection Object
* @param table_name Table name
* @param lob_field_name LOB Field
* @param key_field Primary Key Field
* @param key_value Primary Key value
*/
public String LOB2String(
Connection conn,
String table_name,
String lob_field_name,
String key_field,
String key_value) throws DBAccessException{
try {
BLOB p_BLOB = (BLOB)getBLOB(conn,table_name,lob_field_name,key_field,key_value);
// Open a stream to read BLOB data
InputStream stream = p_BLOB.getBinaryStream(); // Keep BLOB Datas
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.io.OutputStream os = new java.io.BufferedOutputStream(bos); // Read from the BLOB stream and write to the stringbuffer
int nchars = 0; // Number of chanracters read
byte[] l_buffer = new byte[1024]; // Buffer holding characters being transferred
while ((nchars = stream.read(l_buffer)) != -1) // Read from BLOB
os.write(l_buffer,0,nchars);
os.flush();
os.close();
String result = new String(bos.toByteArray());
stream.close(); // Close the BLOB input stream
bos.close();
return result;
} catch (Exception ex) { // Trap SQL and IO errors
try{
conn.rollback();
}catch(SQLException sqle){
}
return null;
}
}
}
lob = ((OracleResultSet)rs).getBLOB(1);or
lob = rs.getBlob(1);
楼主可以试下,跟连接池或jdbc版本有关系。
非常着急,有没有高手现身啊????