在這篇文章里介紹用
JdbcTemplate進行數據庫插入操作,包括對blob或clob字段的插入
?
還有對blob字段的取出操作。
?
1.使用
JdbcTemplate往數據庫里插入數據,其中包含blob字段。
?
- public ? boolean ?doSubmitWeekly( final ?WeeklyVO?weeklyVO)??
- ???????????? throws ?DataAccessException?{??
- ????????StringBuffer?sql?=? new ?StringBuffer();??
- ????????sql.append( "INSERT?INTO?WEEKLY_INFO_T?T?(T.F_START_TIME,?" );??
- ????????sql.append( "?????????????????????????????????T.F_END_TIME,?" );??
- ????????sql.append( "?????????????????????????????????T.F_DATE,?" );??
- ????????sql.append( "?????????????????????????????????T.F_OWNER,?" );??
- ????????sql.append( "?????????????????????????????????T.F_ANNEX_NAME,?" );??
- ????????sql.append( "?????????????????????????????????T.F_ANNEX)?" );??
- ????????sql.append( "??VALUES???(TO_DATE?(?,?'yyyy-mm-dd'),?" );??
- ????????sql.append( "????????????TO_DATE?(?,?'yyyy-mm-dd'),?" );??
- ????????sql.append( "????????????TO_DATE?(to_char(sysdate,'yyyy-mm-dd'),?'yyyy-mm-dd'),?" );??
- ????????sql.append( "?????????????,?" );??
- ????????sql.append( "?????????????,?" );??
- ????????sql.append( "?????????????)?" ); //blob字段 ??
- ??
- ????????Boolean?flag?=? new ?Boolean( false );??
- ??
- ???????? try ?{??
- ????????????flag?=?(Boolean)? this .getJdbcTemplate().execute(sql.toString(),??
- ???????????????????? new ?MyPreparedStatementCallback(weeklyVO));??
- ????????}? catch ?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ???????? return ?flag.booleanValue();??
- ????}??
MyPreparedStatementCallback類的實現
?
- /** ?
- ?*?上傳附件回調操作類 ?
- ?*/ ??
- private ? class ?MyPreparedStatementCallback? implements ??
- ????????PreparedStatementCallback?{??
- ??
- ???? private ?WeeklyVO?weeklyVO;??
- ??
- ???? public ?MyPreparedStatementCallback(WeeklyVO?weeklyVO)?{??
- ???????? this .weeklyVO?=?weeklyVO;??
- ????}??
- ??
- ???? public ?Object?doInPreparedStatement(PreparedStatement?pstm)??
- ???????????? throws ?SQLException,??
- ????????????org.springframework.dao.DataAccessException?{??
- ??
- ????????pstm.setObject( 1 ,? this .weeklyVO.getStartTime());??
- ????????pstm.setObject( 2 ,? this .weeklyVO.getEndTime());??
- ????????pstm.setObject( 3 ,? this .weeklyVO.getOwner());??
- ????????pstm.setObject( 4 ,? this .weeklyVO.getAnnexName());??
- ???????? try ?{??
- ???????????? //?操作Blob?---這里WeeklyVO類的annex屬性是File類型 ??
- ????????????pstm.setBinaryStream( 5 ,? new ?FileInputStream( this .weeklyVO??
- ????????????????????????????.getAnnex()),?( int )?( this .weeklyVO.getAnnex()).length());??
- ???????????? //?操作Clob ??
- ???????????? /** ?
- ????????????pstm.setCharacterStream(5,?new?FileReader(this.weeklyVO ?
- ????????????????????????????.getAnnex()),?(int)?(this.weeklyVO.getAnnex()).length()); ?
- ????????????*/ ??
- ????????}? catch ?(FileNotFoundException?e)?{??
- ????????????e.printStackTrace();??
- ???????????? return ? new ?Boolean( false );??
- ????????}??
- ??
- ???????? try ?{??
- ????????????pstm.execute();??
- ???????????? return ? new ?Boolean( true );??
- ????????}? catch ?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ???????????? return ? new ?Boolean( false );??
- ????????}??
- ????}??
- ??
- }??
2
.使用
JdbcTemplate讀取數據庫中的blob字段信息(把blob內容寫到臨時目錄)
?
- public ?Map?doSelectWeekly(String?weeklyId)? throws ?DataAccessException?{??
- ????????String?sql?=? "select?t.f_annex_name,t.f_annex?from?weekly_info_t?t" ??
- ????????????????+? "?where?t.f_weekly_id?=?" ?+?weeklyId;??
- ????????Map?map?=? new ?HashMap();??
- ????????map?=?(Map)? this .getJdbcTemplate().execute(sql,??
- ???????????????? new ?CallableStatementCallback()?{??
- ??
- ???????????????????? public ?Object?doInCallableStatement(CallableStatement?stmt)??
- ???????????????????????????? throws ?SQLException,??
- ????????????????????????????org.springframework.dao.DataAccessException?{??
- ????????????????????????ResultSet?rs?=?stmt.executeQuery();??
- ????????????????????????Map?map?=? new ?HashMap();??
- ????????????????????????InputStream?inputStream?=? null ;??
- ????????????????????????String?name?=? "" ;??
- ????????????????????????String?path?=?System.getProperty( "java.io.tmpdir" )??
- ????????????????????????????????+? "/" ;??
- ????????????????????????File?temp?=? new ?File(path);??
- ???????????????????????? if ?(!temp.exists())?{??
- ????????????????????????????temp.mkdir();??
- ????????????????????????}??
- ????????????????????????temp?=? null ;??
- ??
- ???????????????????????? while ?(rs.next())?{??
- ????????????????????????????inputStream?=?rs.getBinaryStream( "f_annex" ); //?讀取blob ??
- ??
- ???????????????????? //Reader?fileReader?=?rs.getCharacterStream("f_annex");//?讀取clob ??
- ????????????????????????????name?=?rs.getString( "f_annex_name" );??
- ????????????????????????????path?+=?name;??
- ????????????????????????????File?fileOutput?=? new ?File(path);??
- ??
- ????????????????????????????FileOutputStream?fo;??
- ???????????????????????????? try ?{??
- ????????????????????????????????fo?=? new ?FileOutputStream(fileOutput);??
- ???????????????????????????????? int ?readed;??
- ???????????????????????????????? //?將附件寫到臨時目錄里 ??
- ???????????????????????????????? while ?((readed?=?inputStream.read())?!=?- 1 )?{??
- ????????????????????????????????????fo.write(readed);??
- ????????????????????????????????}??
- ????????????????????????????????fo.close();??
- ????????????????????????????}? catch ?(FileNotFoundException?e)?{??
- ????????????????????????????????e.printStackTrace();??
- ????????????????????????????}? catch ?(IOException?e)?{??
- ????????????????????????????????e.printStackTrace();??
- ????????????????????????????}??
- ????????????????????????}??
- ????????????????????????map.put( "annexName" ,?name);??
- ????????????????????????map.put( "filePath" ,?path);??
- ???????????????????????? return ?map; //返回文件名稱和文件所在路徑,供頁面下載用。 ??
- ????????????????????}??
- ??
- ????????????????});??
- ???????? return ?map;??
- ????}??
附:下載blob內容代碼片段(先把blob內容寫到臨時目錄在從臨時目錄下載)
?
- Map?map?=?weeklyServise.doSelectWeekly( "52" ); //參數為附件ID ??
- String?annexName?=?(String)?map.get( "annexName" );??
- String?path?=?(String)?map.get( "filePath" );??
- ??
- BufferedInputStream?bis?=? null ;??
- BufferedOutputStream?bos?=? null ;??
- OutputStream?fos?=? null ;??
- InputStream?fis?=? null ;??
- ??
- String?filepath?=?path;??
- System.out.println( "文件路徑" ?+?filepath);??
- java.io.File?uploadFile?=? new ?java.io.File(filepath);??
- //從低級流構造成高級流 ??
- fis?=? new ?FileInputStream(uploadFile);??
- bis?=? new ?BufferedInputStream(fis);??
- fos?=?response.getOutputStream();??
- bos?=? new ?BufferedOutputStream(fos);??
- //設置下載文件名 ??
- response.setHeader( "Content-disposition" ,? "attachment;filename=" ??
- ????????+?URLEncoder.encode(annexName,? "utf-8" ));??
- int ?bytesRead?=? 0 ;??
- byte []?buffer?=? new ? byte [ 4096 ];??
- while ?((bytesRead?=?bis.read(buffer,? 0 ,? 4096 ))?!=?- 1 )?{??
- ????bos.write(buffer,? 0 ,?bytesRead); //開始下載數據 ??
- }??
- bos.flush();??
- fis.close();??
- bis.close();??
- fos.close();??
- bos.close();??
- java.io.File?temp?=? new ?java.io.File(System.getProperty( "java.io.tmpdir" )+ "/" );??
- if (temp.isDirectory()){??
- ????FileUtils.deleteDirectory(temp); //刪除臨時文件夾 ??
- }??
- return ? null ;??
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
