当前位置: 技术问答>java相关
在线等待,在jsp中,怎样提交含有blob字段类型的数据表
来源: 互联网 发布时间:2017-05-03
本文导语: 数据库Oracle,某表中一个字段是一张图片的内容,字段类型是Blob,请问该怎样写代码? | /** * 更新数据库的Blob字段。(sqlStr = update tableName set blobField=? where ...) * @param String tabl...
数据库Oracle,某表中一个字段是一张图片的内容,字段类型是Blob,请问该怎样写代码?
|
/**
* 更新数据库的Blob字段。(sqlStr = update tableName set blobField=? where ...)
* @param String tableName 表名
* @param String blobField blob字段名
* @param byte[] blob 更新后blobField的内容
* @param String strPK where 后面的条件,这里是主键字符串
* @return void 返回空
*/
public void updateBlob(String tableName, String blobField, String strPK, byte[] blob) throws Exception
{
if (conn == null)
throw new SQLException("database connection is not availble!");
boolean oldcommit=conn.getAutoCommit();
String strSQLUpdate= "update " + tableName + " set " + blobField + "=EMPTY_BLOB() where " + strPK;
String strSQL= " select " + blobField + " from " + tableName + " where " + strPK + " for update";
try
{
conn.setAutoCommit(false);
PreparedStatement tmpstmt=conn.prepareStatement(strSQLUpdate);
tmpstmt.executeUpdate();
tmpstmt=conn.prepareStatement(strSQL);
ResultSet tmprlt=tmpstmt.executeQuery();
while (tmprlt.next())
{
java.sql.Blob javaBlob = tmprlt.getBlob(blobField);
oracle.sql.BLOB tmpblob = (oracle.sql.BLOB)javaBlob;
OutputStream os= tmpblob.getBinaryOutputStream();
if(blob != null)
os.write(blob);
os.close();
}
if (oldcommit==true)
{
conn.commit();
}
}
catch(Exception e)
{
conn.rollback();
throw e;
}
finally
{
conn.setAutoCommit(oldcommit);
}
}
* 更新数据库的Blob字段。(sqlStr = update tableName set blobField=? where ...)
* @param String tableName 表名
* @param String blobField blob字段名
* @param byte[] blob 更新后blobField的内容
* @param String strPK where 后面的条件,这里是主键字符串
* @return void 返回空
*/
public void updateBlob(String tableName, String blobField, String strPK, byte[] blob) throws Exception
{
if (conn == null)
throw new SQLException("database connection is not availble!");
boolean oldcommit=conn.getAutoCommit();
String strSQLUpdate= "update " + tableName + " set " + blobField + "=EMPTY_BLOB() where " + strPK;
String strSQL= " select " + blobField + " from " + tableName + " where " + strPK + " for update";
try
{
conn.setAutoCommit(false);
PreparedStatement tmpstmt=conn.prepareStatement(strSQLUpdate);
tmpstmt.executeUpdate();
tmpstmt=conn.prepareStatement(strSQL);
ResultSet tmprlt=tmpstmt.executeQuery();
while (tmprlt.next())
{
java.sql.Blob javaBlob = tmprlt.getBlob(blobField);
oracle.sql.BLOB tmpblob = (oracle.sql.BLOB)javaBlob;
OutputStream os= tmpblob.getBinaryOutputStream();
if(blob != null)
os.write(blob);
os.close();
}
if (oldcommit==true)
{
conn.commit();
}
}
catch(Exception e)
{
conn.rollback();
throw e;
}
finally
{
conn.setAutoCommit(oldcommit);
}
}
|
提交的表单主要是enctype="multipart/form-data"属性,如下:
接收的话,如楼上所述:)
接收的话,如楼上所述:)