我的问题是怎么将Blob类型的对象序列化,还有setBytes()有两个方法一个是:
public int setBytes(long pos,byte[] bytes,int offset,int len)另一个是:
public int setBytes(long pos,byte[] bytes)
我没序列化对象,在调用这两个方法时出现异常:
java.sql.SQLException: 不支持的特性
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.dbaccess.DBError.throwUnsupportedFeatureSqlException(DBError.java:689)
at oracle.sql.BLOB.setBytes(BLOB.java:954)
能否告诉我怎么序列化Blob对象成字节数组和使用setBytes()!!
非常感谢上面这个朋友的大力支持,花费您宝贵的休息时间,我再次表示衷心的感谢
You can use a BLOB column to serialize an object into a database. There is a problem serializing object > 4K into the db, but you can get round that in Oracle. You have to use the Oracle JNDI driver, and use class
oracle.sql.BLOB. First INSERT the a new row using the EMPTY_BLOB()
function
e.g.
INSERT INTO blob_table VALUES ( ..., EMPTY_BLOB(), ..)
Then do a SELECT for the BLOB column on that row. Use the ResultSet.getBlob() function to get an oracle.sql.BLOB object, serialze the object to a byte array, use the BLOB.setBytes() function so set the contents of the BLOB. Then you can UPDATE the previously created row, with your BLOB
containing the serialized object
create table tb_file(name varchar(20),detail long row);
然后
File file = new File("aaa.gif");
int fileLength =(int) file.length();
InputStream fin = new FileInputStream(file);
PreparedStatement pstmt = con.prepareStatement("insert into tb_file values('aaa.gif',?)");
pstmt.setBinaryStream (1, fin, fileLength);
pstmt.executeUpdate();
如果你一定要用BLOB存储,你就必须用ORACLE自己的方法:
create table tb_file(name varchar(20),detail BLOB);
con.setAutoCommit(false);
stmt.executeUpdate("insert into tb_file values('aaa.gif',empty_blob())");
下面必须SELECT得到BLOB的对象再向里写:
rs = stmt.executeQuery("select detail from tb_file where name='aaa.gif' for upfdate" );
if(rs.next())
{
Blob blob = rs.getBlob(1);
BinaryOutputStream out = ((oracle.sql.BLOB)blob).getBinaryOutputStream();
byte[] b = new byte[((oracle.sql.BLOB)blob).getBufferSize];
InputStream fin = new FileInputStream(file);
int len = 0;
while( (len = fin.read(b)) != -1)
out.write(b,0,len);
fin.close();
out.close();
con.commit();
}
同样读取数据你并不能象LONG ROW那样
InputStream in = rs.getBinaryInputStream("detail");
而要
Blob blob = rs.getBlob("detail");
in = blob.getBinaryStream();