在这篇文章里介绍用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;
相关推荐
JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate.execute("create table account(" id int primary key auto_increment," + "username varchar(50)," + "balance...
1.针对SpringMVC注解的配置...2.Spring+JdbcTemplate事物管理 3.Spring+Hibernate事物管理 4.Spring实现DWR注解方式的应用配置 5.Spring配置拦截器 6.Spring通过拦截器实现防止重复提交实例 对学习,会起到非常好的效果
全书分3篇共21章,具体内容包括:Spring环境的安装与使用、JSP与JSTL简介、 Spring基础概念与工具、用SpringJdbcTemplate访问数据库、使用Mayen工程、Spring MVC编程、基于 MVC的资源共享网站设计、Spring的AOP编程...
接口JdbcOperations 抽象类JdbcAccessor 核心类 JdbcTemplate extends implements JdbcTemplate类的继承结构具体如下图所示: Spring JdbcTemplate的解析 从JdbcTemplate的继承关系图可以看出,JdbcTemplate类的...
前期项目 Spring+Spring MVC+JDBC Template 增删改查,包含建库,前端加后台
2019年最新最详细的...提供了展现层 SpringMVC和持久层 Spring JDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架
使用maven构建项目,spring mvc,spring,分别与jdbctemplate,hibernate,mybatis全注解整合,其中包括包含有spring动态代理,数据库的事务处理。以及动态数据源的切换! 最新新增了ehcache缓存的应用
Java持久性框架比较该项目比较了Java的非JPA SQL映射(持久性)框架(jOOQ,Spring JDBCTemplate等)的用法。 我们用它来找出在开发时哪个数据库层是最好的我不是在比较性能,而是在日常任务中如何使用这些框架。 我...
│ 02 案例驱动:如何剖析一个 Spring Web 应用程序?.mp4 │ 03 多维配置:如何使用 Spring Boot 中的配置体系?.mp4 │ 05 自动配置:如何正确理解 Spring Boot 自动配置实现原理?.mp4 │ 06 基础规范:如何...
本项目是基于Spring MVC和JdbcTemplate开发的简易设备资产管理系统设计源码,主要使用Java进行开发。项目共包含147个文件,其中XML配置文件86个,Java源代码文件46个,META-INF文件4个,项目构建文件iml和properties...
有很多新手不会分离spring的包而应用到c\s开发中,还在用jdbc来管理事务等繁琐的数据管理。 吾将spring2.5的包分离出来欲给一些人以方便。只需下载该包添加到项目中引用即可,如果单独用spring管理事务,记住用...
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...
2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...
这对于开发需要高并发性能的应用程序(如服务器端应用、网络应用等)非常重要。 自动内存管理(垃圾回收): Java具有自动内存管理机制,通过垃圾回收器自动回收不再使用的对象,使得开发者不需要手动管理内存,...
17 Spring Boot Web(三)38:48 18 Spring Boot 定制和优化内嵌的Tomcat35:14 --定制和优化Tomcat,以编码的方式设置Tomcat的各个属性值,以及Tomcat的日志配置 19 Spring Boot JDBC38:38 --多种数据源的配置、...
SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物。本文重点给大家介绍spring boot 使用JdbcTemplate访问数据库,需要的朋友可以参考下
spring-jdbctemplate-example 这是一个Spring Monolith应用程序示例,该示例使用jdbcTemplate连接MySQL数据库并执行反馈操作的操作。测试和构建运行测试(也运行大型测试) ./gradlew clean test 有条件地根据测试...
博客论坛系统(molihub)是一个基于 Spring、SpringMVC、JDBCTemplate、JSP 开发的 Java Web 项目。项目页面美观大气,完成度较高,前后端不分离的单体架构,非常适合刚刚接触学习 Spring 的技术小白学习,也非常...
使用maven构建项目,spring mvc,spring,分别与jdbctemplate,hibernate,mybatis全注解整合,其中包括包含有spring动态代理,数据库的事务处理。以及动态数据源的切换! 最新新增了ehcache缓存的应用 ...