以下示例将演示使用spring jdbc更新BLOB类型的字段值,即更新student表中的可用记录。

student表的结构如下 -

CREATE TABLE student(
ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(20) NOT NULL,
AGE INT NOT NULL,
IMAGE BLOB,
PRIMARY KEY (ID)
); INSERT INTO student(ID,NAME,AGE,IMAGE) VALUES(1,'Maxsu', 23, NULL);
SQL

语法:

MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("id", id);
in.addValue("image", new SqlLobValue(new ByteArrayInputStream(imageData),
imageData.length, new DefaultLobHandler()), Types.BLOB);
String SQL = "update Student set image = :image where id = :id";
NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource);
jdbcTemplateObject.update(SQL, in);
Java

在上面语法中 -

  • SqlLobValue - 表示SQL BLOB / CLOB值参数的对象。
  • in - SqlParameterSource对象将参数传递给更新查询。
  • jdbcTemplateObject - NamedParameterJdbcTemplate对象来更新数据库中的学生对象。

创建项目

要了解上面提到的Spring JDBC相关的概念,这里创建一个项目用来演示如何操作BLOB类型字段。打开Eclipse IDE,并按照以下步骤创建一个名称为:HandlingBLOB 的Spring应用程序项目。

步骤说明

  1. 参考第一个Spring JDBC项目来创建一个Maven项目 - http://www.yiibai.com/springjdbc/first_application.html
  2. 更新bean配置并运行应用程序。

完整的项目结构如下所示 -

文件 : pom.xml 的内容 -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yiibai</groupId>
<artifactId>HandlingBLOB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HandlingBLOB</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

以下是数据访问对象接口文件:StudentDAO.java的代码内容:

package com.yiibai;

import java.util.List;
import javax.sql.DataSource; public interface StudentDAO {
/**
* This is the method to be used to initialize database resources ie.
* connection.
*/
public void setDataSource(DataSource ds); /**
* This is the method to be used to update a record into the Student table.
*/
public void updateImage(Integer id, byte[] imageData); }
Java

以下是文件:Student.java的代码内容:

package com.yiibai;

public class Student {
private Integer age;
private String name;
private Integer id;
private byte[] image; public void setAge(Integer age) {
this.age = age;
} public Integer getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public String getName() {
return name;
} public void setId(Integer id) {
this.id = id;
} public Integer getId() {
return id;
} public byte[] getImage() {
return image;
} public void setImage(byte[] image) {
this.image = image;
}
}
Java

以下是文件:StudentMapper.java的代码内容:

package com.yiibai;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper; public class StudentMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setImage(rs.getBytes("image"));
return student;
}
}
Java

以下是实现类文件:StudentJDBCTemplate.java,它实现了接口StudentDAO.java
以下是文件:StudentJDBCTemplate.java的代码内容:

package com.yiibai;

import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.support.SqlLobValue;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import java.io.ByteArrayInputStream;
import java.sql.Types; public class StudentJDBCTemplate implements StudentDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} public void updateImage(Integer id, byte[] imageData) {
MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("id", id);
in.addValue("image", new SqlLobValue(new ByteArrayInputStream(imageData),
imageData.length, new DefaultLobHandler()), Types.BLOB); String SQL = "update Student set image = :image where id = :id"; NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource);
jdbcTemplateObject.update(SQL, in);
System.out.println("Updated Record with ID = " + id );
}
}
Java

以下是程序执行入口文件:MainApp.java的代码内容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.StudentJDBCTemplate; public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-beans.xml"); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate"); byte[] imageData = { 0, 1, 0, 8, 20, 40, 95 };
studentJDBCTemplate.updateImage(1, imageData);
}
}
Java

以下是Bean和数据库配置文件:application-beans.xml的代码内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean> <!-- Definition for studentJDBCTemplate bean -->
<bean id="studentJDBCTemplate" class="com.yiibai.StudentJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean> </beans>
XML

注意: application-beans.xml 文件的位置是:{workspace}/fistapp/src/main/java 目录,如果放置错了,程序可能会因为找不到此配置文件而出错。

完成创建源代码和bean和数据库连接信息的文件配置后,运行应用程序。这里先简单说明一下运行的步骤,在项目名称(HandlingBLOB)上点击右键,在弹出的菜单中选择:【Run As】-> 【Maven test】

在运行测试成功后,应该会输出类似以下内容(含有 BUILD SUCCESS 的信息) 。
接下来,点击类文件:MainApp.java 选择【Run As】->【Java Application】,如果应用程序一切正常,这将打印以下消息:

Updated Record with ID = 1

Spring JDBC处理BLOB类型字段的更多相关文章

  1. Spring JDBC处理CLOB类型字段

    以下示例将演示使用spring jdbc更新CLOB类型的字段值,即更新student表中的可用记录. student表的结构如下 - CREATE TABLE student( ID INT NOT ...

  2. 【JDBC核心】操作 BLOB 类型字段

    操作 BLOB 类型字段 MySQL BLOB 类型 MySQL 中,BLOB 是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据. 插入 BLOB 类型的数据必须使用 Pre ...

  3. mybatis oracle BLOB类型字段保存与读取

    一.BLOB字段 BLOB是指二进制大对象也就是英文Binary Large Object的所写,而CLOB是指大字符对象也就是英文Character Large Object的所写.其中BLOB是用 ...

  4. Oracle中,如何将String插入到BLOB类型字段

    1,String插入到BLOB类型字段,(这里的字符串以生成的XML为例): String XML = document.asXML();  //使用dom4j写成的xml是String类型,记得st ...

  5. oracle中如何判断blob类型字段是否为空

    eg.假如有表T_GA_GRJBXX  ,字段zp是blob类型 查询blob非空的记录 SELECT * FROM u_rs_sjgx.T_GA_GRJBXX TB WHERE TB.zp IS n ...

  6. PDM->OOM->C#实体类生成时,对Blob类型字段的处理

    pdm中的Blob字段生成OOM时,自动变成了string类型,再生成实体类时也是string 如何将oom中对应的blob字段设置为Byte[]类型,目前没找到方法, 只能通过脚本,将生成后的OOM ...

  7. (转载)VB 查询Oracle中blob类型字段,并且把blob中的图片以流的方式显示在Image上

    原文摘自:http://heisetoufa.iteye.com/blog/ '模块代码 Private Declare Function CreateStreamOnHGlobal Lib &quo ...

  8. jdbc获取blob类型乱码

    一.使用场景: mysql数据库字段类型为longblob,在数据库里看中文字符正常,java读取字串的时候发现中文乱码 使用到了activeMq 二.排查: (1)修改eclipse的环境编码为ut ...

  9. oracle 11g r2 blob类型getString报错问题

    摘要: 问题: 在hibernate中实体类中blob类型字段为 private String textBlob; 查询时报错: java.sql.SQLException: 无效的列类型: getS ...

随机推荐

  1. Spark VS Presto VS Impala

    https://www.quora.com/What-is-the-difference-between-Spark-and-Presto

  2. Unable to load configuration. - [unknown location]

    严重: Exception starting filter StrutsPrepareFilterUnable to load configuration. - [unknown location] ...

  3. angular学习笔记(三十)-指令(4)-transclude

    本篇主要介绍指令的transclude属性: transclude的值有三个: 1.transclude:false(默认值) 不启用transclude功能. 2.transclude:true 启 ...

  4. HTTP 代理服务器技术选型之旅

    HTTP 代理服务器技术选型之旅 背景 长期以来,贴吧开发人员多,业务耦合大,需求变化频繁,因此容易产生 bug.而我所负责的广告相关业务,和 UI 密切相关,一旦因为某种原因(甚至是被别人改了代码) ...

  5. 【Linux技术】磁盘的物理组织,深入理解文件系统

    磁盘即是硬盘,由许多块盘片(盘面)组成,每个盘片的上下两面都涂有磁粉,磁化后可以存储信息数据.每个盘片的上下两面都安装有磁头,磁头被安装在梳状的可以做直线运动的小车上以便寻道,每个盘面被格式化成有若干 ...

  6. hive的row_number()函数

    hive的row_number()函数 功能 用于分组,比方说依照uuid分组 组内可以依照某个属性排序,比方说依照uuid分组,组内按照imei排序 语法为row_number() over (pa ...

  7. c++ primer读书笔记之c++11(三)

    1 =default构造函数限定符 c++11针对构造函数提供了=default限定符,可以用于显式指定编译器自动生成特定的构造函数.析构或赋值运算函数.参考代码如下: class CtorDftTy ...

  8. jetty debug修改 java static 静态变量值不会生效

    在jetty debug模式下修改static静态变量值不会重新Load 因为jetty是嵌入式web容器,static静态变量是全局的,如果想生效,就必须重启jetty 在热部署的时候tomcat会 ...

  9. sql2008破解加密存储过程

    网上的很多不能正确解密,出现空白,还好有这个,mark下了. Create PROCEDURE [dbo].[sp_windbidecrypt] (@procedure sysname = NULL, ...

  10. Sublime Text2/3怎样在Mac OSX中配置CTags插件

    参考地址: http://jingyan.baidu.com/article/48206aeafba820216ad6b3f5.html