java如何存储blob

ypchen79 2005-01-18 05:29:03
现在遇到一个难题,请dx们赐教:
我现在需要将一个很大的二进制数组存储到oracle数据库中,对应的数据库字段类型是blob,是一个insert操作。
代码片度如下:
String sql = "insert into FCDB_BACKUP VALUES(?,?,?,TO_DATE('" +
generateBackupTime() + "','YYYY-MM-DD HH24:MI:SS'))";
psBackup = conn.prepareStatement(sql);
for (int i = 0; i < list.getListSize(); i++)
{
byte[] b1 = list.getKeyAt(i);
byte[] b2 = list.getValueAt(i);
String key = null;
try
{
key = new String(b1, "UTF-8");
}
catch (Exception ex2)
{
ex2.printStackTrace();
}
BLOB blob = BLOB.empty_lob();
OutputStream os = blob.getBinaryOutputStream();
os.write(b2);
psBackup.setInt(1, tid);
psBackup.setString(2, key);
psBackup.setBlob(3, blob);
psBackup.addBatch();
}
psBackup.executeBatch();
conn.commit();
执行之后会出现“java.sql.SQLException: 空二进制大对象操作无效”的异常,如果注释掉
OutputStream os = blob.getBinaryOutputStream();
os.write(b2);
部分,则没有错误。
请问dx们,有没有比较方便的方式能够一次性将blob数据写入数据库,因为这是一个内存数据库到关系数据库的备份操作,数据量也比较大。
...全文
744 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyi8903 2005-01-20
  • 打赏
  • 举报
回复
唉!打自己个耳光.

看下面的.假设你的blob字段有默认值Empty_BLOB()

/**
* 将文件写入数据库
* @param pathname:文件全路径
*/
public void writeDBFromFile(String pathname) {
myFile = new File(pathname);
try {
//将文件转化为文件流
java.io.FileInputStream fis = new FileInputStream(myFile);
//得到一个数据库连接
con = new DBconn().getConnection();
pst = con.prepareStatement("update test set mblob=? where id=2");
//pst.setInt(1,2);
byte bb[];
String sql = "ab";
bb = sql.getBytes();
pst.setBinaryStream(1, new java.io.ByteArrayInputStream(bb), bb.length);
//pst.setBinaryStream(1,fis,(int)myFile.length());
pst.executeUpdate();
}
catch (Exception e) {
System.out.println(e.toString());
}
finally {
try {
if (rs != null)
rs.close();
if (pst != null)
pst.close();
if (con != null)
con.close();

}
catch (Exception e) {
System.out.println(e.toString());
}
}
}



否则你就先插入一个空值Empty_BLOB()
liuyi8903 2005-01-20
  • 打赏
  • 举报
回复
我帖错了.我上面帖的代码是取大对象的.

以下的才对.
java.sql.Blob myBlob = getBlob(sql);
byte[] content = (byte[]) BeanProcessor.getPropertyValueObject(blobName, o);
DBconn db = null;
try {
db = new DBconn();
db.getConnection().setAutoCommit(false);
java.io.OutputStream os =
( (weblogic.jdbc.common.OracleBlob) myBlob).getBinaryOutputStream();
if (content != null && content.length > 0) {

*32768);
os.write(content,0,content.length);
}
os.flush();
os.close();
db.commit();
}
catch (SQLException e) {
throw new DAOException("DAO层新增大对象操作处理时数据库操作出错!");
}
catch (java.io.IOException e) {
throw new DAOException(e, "DAO层新增大对象操作处理时数据库连接出错!");
}catch(ConnectionException e){
throw new DAOException(e, "DAO层新增大对象操作处理时数据库连接出错!");
}
newboy3205579 2005-01-19
  • 打赏
  • 举报
回复
不是这样的,现插入一个空的blob,然后才能插入
liuyi8903 2005-01-19
  • 打赏
  • 举报
回复
你用的应用服务器是什么,连接方式是什么?

我给你提供一个基于weblogic连接池的,因为不同的服务器可能会有点区别
public java.io.ByteArrayOutputStream readFileFromDB(int index, String ifwhere) {

InputStream is = null;
int bytesread = 0;
byte[] butter = new byte[8 * 1024];
java.io.ByteArrayOutputStream bos = null;
String sql = "";
java.sql.Blob blob = null;
sql = sqlblob[index] + ifwhere;
// System.out.println(sql);
//
// System.out.println(sql);
try {
//Initialization ByteArrayOutputStream
bos = new java.io.ByteArrayOutputStream();
//Get Connection
con = new DBconn().getConnection();

pst = con.prepareStatement(sql);
rs = pst.executeQuery();
int size = 0;
while (rs.next()) {
//Get Blob
blob = rs.getBlob(1);
if (blob != null && blob.length() != 0) {
is = blob.getBinaryStream();
while ( ( (bytesread = is.read()) != -1)) {
bos.write(bytesread);
}

}
}

}
catch (Exception e) {
System.out.println(e.toString());
}
finally {
try {
if (rs != null)
rs.close();
if (pst != null)
pst.close();
if (con != null)
con.close();

}
catch (Exception e) {
System.out.println("数据库连接错误:" + e.toString());
}
}
return bos;
}

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧