字符串如何存到数据库blob字段(二)

2014-11-24 10:16:54 · 作者: · 浏览: 1
s = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //从数据库中一段一段的读出数据
//System.out.println(size);
if(size != -1) //-1表示读到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}

}
catch(Exception e)
{
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
finally
{
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}

public static void main(String[] args)
{
try
{

BlobPros blob = new BlobPros();
//blob.blobInsert("C:\\Downloads\\luozsh1.jpg");
blob.blobRead("c:/downloads/luozishang.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}

字符串如何存到数据库blob字段?
存取图形用输入流

字符串如何存到数据库blob字段?
字符串如何存到数据库blob字段?
[java]
//存取二进制文件到BLOB字段的函数
import oracle.sql.BLOB;
import oracle.jdbc.driver.OracleResultSet;

public void updateBlob(Connection conn,String strFile,long newId,String table_name) {
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
//stmt.execute("insert into A(A,B) values ('"+strId+"',empty_blob())");
ResultSet rset = stmt.executeQuery("select t.content from "+table_name +" t where id=" + newId +
" FOR UPDATE"); www.2cto.com
BLOB blob = null;
while (rset.next()) {
blob = ( (OracleResultSet) rset).getBLOB(1);
}
File binaryFile = new File(strFile);
FileInputStream instream = new FileInputStream(binaryFile); //读入二进制文件
OutputStream outstream = blob.getBinaryOutputStream();
byte[] buffer = new byte[32];

int length = -1;
while ( (length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();

conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}