oracle的倒是试过,大概是这样的,可能记得不是很清楚,你自己试试。 可以分为两步 1、先插入一个空的blob,例如insert into test(name,content) values('001',empty_blob()) 2、执行一个更新,例如 pstmt = conn.prepareStatement("update javatest set content=? where name=?"); pstmt.setBlob(1,blob); pstmt.setString(2,"001"); pstmt.executeUpdate();
PreparedStatementsetBinaryStream public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLExceptionSets the designated parameter to the given input stream, which will have the specified number of bytes. When a very large binary value is input to a LONGVARBINARY parameter, it may be more practical to send it via a java.io.InputStream object. The data will be read from the stream as needed until end-of-file is reached. Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface. Parameters: parameterIndex - the first parameter is 1, the second is 2, ... x - the java input stream which contains the binary parameter value length - the number of bytes in the stream Throws: SQLException - if a database access error occurs
Oracle ..con.setAutoCommit(false); String prefix = new SimpleDateFormat("MMddHHmmss").format(new java.util.Date()); // 先插入一条记录,用于后面获得一个Blob对象 pstmt = con.prepareStatement("insert into javatest(PICNAME,content) values(?,empty_blob())"); pstmt.setString(1,prefix+i); pstmt.executeUpdate(); pstmt.close();// 获得Blob对象(由于Blob是接口,不能实例化,所以只好用这种曲折方法获得)(FOR UPDATE NOWAIT 需要加上 pstmt = con.prepareStatement("SELECT content FROM JAVATEST WHERE trim(PICNAME)=? FOR UPDATE NOWAIT"); pstmt.setString(1,prefix+i); ResultSet rset = pstmt.executeQuery();if (rset.next()) blob = (oracle.sql.BLOB)rset.getBlob(1); pstmt.close();if (blob == null) { addErr("插入图象数据到数据库失败!"); con.close(); return ; }// 填充Blob值,用于提交到数据库 FileInputStream fin = new FileInputStream(imgs[i]); OutputStream out = blob.getBinaryOutputStream(); byte[] data = new byte[fin.available()];// 获取 fin.read(data); out.write(data);
// 关闭资源 fin.close(); out.close();
// 插入数据库 pstmt = con.prepareStatement("update javatest set content=? where PICNAME=?"); pstmt.setBlob(1,blob); pstmt.setString(2,prefix+i); pstmt.executeUpdate(); pstmt.close(); con.commit();
InputStream in =request.getInputStream(); conn=dbcon.getConnection();//获得数据库连接 ps=conn.prepareStatement("insert into imageTable values(?,?)"); ps.setString(1,num);//num:是序号 ps.setBinaryStream(2,in,in.available()); ps.executeUpdate(); in.close(); ps.close(); conn.close();
可以分为两步
1、先插入一个空的blob,例如insert into test(name,content) values('001',empty_blob())
2、执行一个更新,例如
pstmt = conn.prepareStatement("update javatest set content=? where name=?");
pstmt.setBlob(1,blob);
pstmt.setString(2,"001");
pstmt.executeUpdate();
public void setBinaryStream(int parameterIndex,
InputStream x,
int length)
throws SQLExceptionSets the designated parameter to the given input stream, which will have the specified number of bytes. When a very large binary value is input to a LONGVARBINARY parameter, it may be more practical to send it via a java.io.InputStream object. The data will be read from the stream as needed until end-of-file is reached.
Note: This stream object can either be a standard Java stream object or your own subclass that implements the standard interface.
Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the java input stream which contains the binary parameter value
length - the number of bytes in the stream
Throws:
SQLException - if a database access error occurs
String prefix = new SimpleDateFormat("MMddHHmmss").format(new java.util.Date());
// 先插入一条记录,用于后面获得一个Blob对象
pstmt = con.prepareStatement("insert into javatest(PICNAME,content) values(?,empty_blob())");
pstmt.setString(1,prefix+i);
pstmt.executeUpdate();
pstmt.close();// 获得Blob对象(由于Blob是接口,不能实例化,所以只好用这种曲折方法获得)(FOR UPDATE NOWAIT 需要加上
pstmt = con.prepareStatement("SELECT content FROM JAVATEST WHERE trim(PICNAME)=? FOR UPDATE NOWAIT");
pstmt.setString(1,prefix+i);
ResultSet rset = pstmt.executeQuery();if (rset.next()) blob = (oracle.sql.BLOB)rset.getBlob(1);
pstmt.close();if (blob == null)
{
addErr("插入图象数据到数据库失败!");
con.close();
return ;
}// 填充Blob值,用于提交到数据库
FileInputStream fin = new FileInputStream(imgs[i]);
OutputStream out = blob.getBinaryOutputStream();
byte[] data = new byte[fin.available()];// 获取
fin.read(data);
out.write(data);
// 关闭资源
fin.close();
out.close();
// 插入数据库
pstmt = con.prepareStatement("update javatest set content=? where PICNAME=?");
pstmt.setBlob(1,blob);
pstmt.setString(2,prefix+i);
pstmt.executeUpdate();
pstmt.close();
con.commit();
InputStream in =request.getInputStream();
conn=dbcon.getConnection();//获得数据库连接
ps=conn.prepareStatement("insert into imageTable values(?,?)");
ps.setString(1,num);//num:是序号
ps.setBinaryStream(2,in,in.available());
ps.executeUpdate();
in.close();
ps.close();
conn.close();