首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Spring Sample里面的ImageDB有关问题

2012-11-03 
Spring Sample里面的ImageDB问题看到Spring文件夹里面的ImageDB应用例子,就想跑起来看看效果怎样。如果使用

Spring Sample里面的ImageDB问题
看到Spring文件夹里面的ImageDB应用例子,就想跑起来看看效果怎样。
如果使用Mysql数据库,修改一下DDL里面的content字段为LONGBLOB就可以很方便地将图片存储在MySql数据库中。
(MySQL的四种BLOB类型
类型  大小(单位:字节)
TinyBlob  最大 255B
Blob  最大 65K
MediumBlob  最大 16M
LongBlob  最大 4G

在Oracle数据库(我用的是Oracle 11g XE)中却不是成功保存图片,一点击上传应用就卡住了,在Eclipse的高度窗口下,出现在很多Daemon进程。(一时找不出原因,我想可能是Oracle数据库版本以及Spring2.5对BLOB的一些兼容性上的问题。非常欢迎高人指正这个猜测)
于是我修改了一下存储图片的方法为如下,就可正常地在Oracle中存储图片了。

@Transactionalpublic void storeImage(final String name, final InputStream contentStream,final int contentLength, final String description)throws DataAccessException {if (lobHandler instanceof OracleLobHandler) {try {PreparedStatement ptst = getConnection().prepareStatement("INSERT INTO IMAGEDB VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())");ptst.setString(1, name);ptst.executeUpdate();// Execute SQL statementptst = getConnection().prepareStatement("SELECT CONTENT ,DESCRIPTION FROM IMAGEDB WHERE IMAGE_NAME = ? FOR UPDATE");ptst.setString(1, name);ResultSet rs = ptst.executeQuery();rs.next();Blob contentBlob = rs.getBlob(1);OutputStream blobOutputStream = ((BLOB) contentBlob).getBinaryOutputStream();FileCopyUtils.copy(contentStream, blobOutputStream);Clob descClob = rs.getClob(2);Writer clobWriter = ((oracle.sql.CLOB) descClob).getCharacterOutputStream();clobWriter.write(description);clobWriter.close();contentStream.close();blobOutputStream.close();getConnection().commit();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {getJdbcTemplate().execute("INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {protected void setValues(PreparedStatement ps,LobCreator lobCreator)throws SQLException {ps.setString(1, name);lobCreator.setBlobAsBinaryStream(ps, 2,contentStream, contentLength);lobCreator.setClobAsString(ps, 3,description);}});}}

热点排行