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

Spring jdbcTemplate施用(三)

2013-07-11 
Spring jdbcTemplate应用(三)在这篇文章里介绍用JdbcTemplate进行数据库插入操作,包括对blob或clob字段的

Spring jdbcTemplate应用(三)

在这篇文章里介绍用JdbcTemplate进行数据库插入操作,包括对blob或clob字段的插入Java代码

  1. public?boolean?doSubmitWeekly(final?WeeklyVO?weeklyVO)??
  2. ????????????throws?DataAccessException?{??
  3. ????????StringBuffer?sql?=?new?StringBuffer();??
  4. ????????sql.append("INSERT?INTO?WEEKLY_INFO_T?T?(T.F_START_TIME,?");??
  5. ????????sql.append("?????????????????????????????????T.F_END_TIME,?");??
  6. ????????sql.append("?????????????????????????????????T.F_DATE,?");??
  7. ????????sql.append("?????????????????????????????????T.F_OWNER,?");??
  8. ????????sql.append("?????????????????????????????????T.F_ANNEX_NAME,?");??
  9. ????????sql.append("?????????????????????????????????T.F_ANNEX)?");??
  10. ????????sql.append("??VALUES???(TO_DATE?(?,?'yyyy-mm-dd'),?");??
  11. ????????sql.append("????????????TO_DATE?(?,?'yyyy-mm-dd'),?");??
  12. ????????sql.append("????????????TO_DATE?(to_char(sysdate,'yyyy-mm-dd'),?'yyyy-mm-dd'),?");??
  13. ????????sql.append("?????????????,?");??
  14. ????????sql.append("?????????????,?");??
  15. ????????sql.append("?????????????)?");//blob字段??
  16. ??
  17. ????????Boolean?flag?=?new?Boolean(false);??
  18. ??
  19. ????????try?{??
  20. ????????????flag?=?(Boolean)?this.getJdbcTemplate().execute(sql.toString(),??
  21. ????????????????????new?MyPreparedStatementCallback(weeklyVO));??
  22. ????????}?catch?(Exception?e)?{??
  23. ????????????e.printStackTrace();??
  24. ????????}??
  25. ????????return?flag.booleanValue();??
  26. ????}??




MyPreparedStatementCallback类的实现Java代码

  1. /**?
  2. ?*?上传附件回调操作类?
  3. ?*/??
  4. private?class?MyPreparedStatementCallback?implements??
  5. ????????PreparedStatementCallback?{??
  6. ??
  7. ????private?WeeklyVO?weeklyVO;??
  8. ??
  9. ????public?MyPreparedStatementCallback(WeeklyVO?weeklyVO)?{??
  10. ????????this.weeklyVO?=?weeklyVO;??
  11. ????}??
  12. ??
  13. ????public?Object?doInPreparedStatement(PreparedStatement?pstm)??
  14. ????????????throws?SQLException,??
  15. ????????????org.springframework.dao.DataAccessException?{??
  16. ??
  17. ????????pstm.setObject(1,?this.weeklyVO.getStartTime());??
  18. ????????pstm.setObject(2,?this.weeklyVO.getEndTime());??
  19. ????????pstm.setObject(3,?this.weeklyVO.getOwner());??
  20. ????????pstm.setObject(4,?this.weeklyVO.getAnnexName());??
  21. ????????try?{??
  22. ????????????//?操作Blob?---这里WeeklyVO类的annex属性是File类型??
  23. ????????????pstm.setBinaryStream(5,?new?FileInputStream(this.weeklyVO??
  24. ????????????????????????????.getAnnex()),?(int)?(this.weeklyVO.getAnnex()).length());??
  25. ????????????//?操作Clob??
  26. ????????????/**?
  27. ????????????pstm.setCharacterStream(5,?new?FileReader(this.weeklyVO?
  28. ????????????????????????????.getAnnex()),?(int)?(this.weeklyVO.getAnnex()).length());?
  29. ????????????*/??
  30. ????????}?catch?(FileNotFoundException?e)?{??
  31. ????????????e.printStackTrace();??
  32. ????????????return?new?Boolean(false);??
  33. ????????}??
  34. ??
  35. ????????try?{??
  36. ????????????pstm.execute();??
  37. ????????????return?new?Boolean(true);??
  38. ????????}?catch?(Exception?e)?{??
  39. ????????????e.printStackTrace();??
  40. ????????????return?new?Boolean(false);??
  41. ????????}??
  42. ????}??
  43. ??
  44. }??




2.使用JdbcTemplate读取数据库中的blob字段信息(把blob内容写到临时目录)Java代码

  1. public?Map?doSelectWeekly(String?weeklyId)?throws?DataAccessException?{??
  2. ????????String?sql?=?"select?t.f_annex_name,t.f_annex?from?weekly_info_t?t"??
  3. ????????????????+?"?where?t.f_weekly_id?=?"?+?weeklyId;??
  4. ????????Map?map?=?new?HashMap();??
  5. ????????map?=?(Map)?this.getJdbcTemplate().execute(sql,??
  6. ????????????????new?CallableStatementCallback()?{??
  7. ??
  8. ????????????????????public?Object?doInCallableStatement(CallableStatement?stmt)??
  9. ????????????????????????????throws?SQLException,??
  10. ????????????????????????????org.springframework.dao.DataAccessException?{??
  11. ????????????????????????ResultSet?rs?=?stmt.executeQuery();??
  12. ????????????????????????Map?map?=?new?HashMap();??
  13. ????????????????????????InputStream?inputStream?=?null;??
  14. ????????????????????????String?name?=?"";??
  15. ????????????????????????String?path?=?System.getProperty("java.io.tmpdir")??
  16. ????????????????????????????????+?"/";??
  17. ????????????????????????File?temp?=?new?File(path);??
  18. ????????????????????????if?(!temp.exists())?{??
  19. ????????????????????????????temp.mkdir();??
  20. ????????????????????????}??
  21. ????????????????????????temp?=?null;??
  22. ??
  23. ????????????????????????while?(rs.next())?{??
  24. ????????????????????????????inputStream?=?rs.getBinaryStream("f_annex");//?读取blob??
  25. ??
  26. ????????????????????//Reader?fileReader?=?rs.getCharacterStream("f_annex");//?读取clob??
  27. ????????????????????????????name?=?rs.getString("f_annex_name");??
  28. ????????????????????????????path?+=?name;??
  29. ????????????????????????????File?fileOutput?=?new?File(path);??
  30. ??
  31. ????????????????????????????FileOutputStream?fo;??
  32. ????????????????????????????try?{??
  33. ????????????????????????????????fo?=?new?FileOutputStream(fileOutput);??
  34. ????????????????????????????????int?readed;??
  35. ????????????????????????????????//?将附件写到临时目录里??
  36. ????????????????????????????????while?((readed?=?inputStream.read())?!=?-1)?{??
  37. ????????????????????????????????????fo.write(readed);??
  38. ????????????????????????????????}??
  39. ????????????????????????????????fo.close();??
  40. ????????????????????????????}?catch?(FileNotFoundException?e)?{??
  41. ????????????????????????????????e.printStackTrace();??
  42. ????????????????????????????}?catch?(IOException?e)?{??
  43. ????????????????????????????????e.printStackTrace();??
  44. ????????????????????????????}??
  45. ????????????????????????}??
  46. ????????????????????????map.put("annexName",?name);??
  47. ????????????????????????map.put("filePath",?path);??
  48. ????????????????????????return?map;//返回文件名称和文件所在路径,供页面下载用。??
  49. ????????????????????}??
  50. ??
  51. ????????????????});??
  52. ????????return?map;??
  53. ????}??




附:下载blob内容代码片段(先把blob内容写到临时目录在从临时目录下载)Java代码

  1. Map?map?=?weeklyServise.doSelectWeekly("52");//参数为附件ID??
  2. String?annexName?=?(String)?map.get("annexName");??
  3. String?path?=?(String)?map.get("filePath");??
  4. ??
  5. BufferedInputStream?bis?=?null;??
  6. BufferedOutputStream?bos?=?null;??
  7. OutputStream?fos?=?null;??
  8. InputStream?fis?=?null;??
  9. ??
  10. String?filepath?=?path;??
  11. System.out.println("文件路径"?+?filepath);??
  12. java.io.File?uploadFile?=?new?java.io.File(filepath);??
  13. //从低级流构造成高级流??
  14. fis?=?new?FileInputStream(uploadFile);??
  15. bis?=?new?BufferedInputStream(fis);??
  16. fos?=?response.getOutputStream();??
  17. bos?=?new?BufferedOutputStream(fos);??
  18. //设置下载文件名??
  19. response.setHeader("Content-disposition",?"attachment;filename="??
  20. ????????+?URLEncoder.encode(annexName,?"utf-8"));??
  21. int?bytesRead?=?0;??
  22. byte[]?buffer?=?new?byte[4096];??
  23. while?((bytesRead?=?bis.read(buffer,?0,?4096))?!=?-1)?{??
  24. ????bos.write(buffer,?0,?bytesRead);//开始下载数据??
  25. }??
  26. bos.flush();??
  27. fis.close();??
  28. bis.close();??
  29. fos.close();??
  30. bos.close();??
  31. java.io.File?temp?=?new?java.io.File(System.getProperty("java.io.tmpdir")+"/");??
  32. if(temp.isDirectory()){??
  33. ????FileUtils.deleteDirectory(temp);//删除临时文件夹??
  34. }??
  35. return?null;??

?

热点排行