`

Let’s Review How to Insert Clob or Blob via JDBC

 
阅读更多

最近在使用JDBC来访问数据,处理LOB字段的时候出现了问题,Google了一下,发现下面写得比较好的文章,非常值得借鉴,文章出处:

https://blog.jooq.org/2015/04/27/lets-review-how-to-insert-clob-or-blob-via-jdbc/

 

LOBs are a PITA in all databases, as well as in JDBC. Handling them correctly takes a couple of lines of code, and you can be sure that you’ll get it wrong eventually. Because you have to think of a couple of things:

  • Foremost, LOBs are heavy resources that need special lifecycle management. Once you’ve allocated a LOB, you better “free” it as well to decrease the pressure on your GC. This article shows more about why you need to free lobs
  • The time when you allocate and free a lob is crucial. It might have a longer life span than any of your ResultSetPreparedStatement, orConnection / transaction. Each database manages such life spans individually, and you might have to read up the specifications in edge cases
  • While you may use String instead of Clob, or byte[] instead of Blobfor small to medium size LOBs, this may not always be the case, and may even lead to some nasty errors, like Oracle’s dreaded ORA-01461: can bind a LONG value only for insert into a LONG column

So, if you’re working on a low level using JDBC (instead of abstracting JDBC via Hibernate or jOOQ), you better write a small utility that takes care of proper LOB handling.

We’ve recently re-discovered our own utility that we’re using for jOOQ integration testing, at least in some databases, and thought this might be very useful to a couple of our readers who operate directly with JDBC. Consider the following class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class LOB implements AutoCloseable {
 
    private final Connection connection;
    private final List<Blob> blobs;
    private final List<Clob> clobs;
 
    public LOB(Connection connection) {
        this.connection = connection;
        this.blobs = new ArrayList<>();
        this.clobs = new ArrayList<>();
    }
 
    public final Blob blob(byte[] bytes)
    throws SQLException {
        Blob blob;
 
        // You may write more robust dialect
        // detection here
        if (connection.getMetaData()
                      .getDatabaseProductName()
                      .toLowerCase()
                      .contains("oracle")) {
            blob = BLOB.createTemporary(connection,
                       false, BLOB.DURATION_SESSION);
        }
        else {
            blob = connection.createBlob();
        }
 
        blob.setBytes(1, bytes);
        blobs.add(blob);
        return blob;
    }
 
    public final Clob clob(String string)
    throws SQLException {
        Clob clob;
 
        if (connection.getMetaData()
                      .getDatabaseProductName()
                      .toLowerCase()
                      .contains("oracle")) {
            clob = CLOB.createTemporary(connection,
                       false, CLOB.DURATION_SESSION);
        }
        else {
            clob = connection.createClob();
        }
 
        clob.setString(1, string);
        clobs.add(clob);
        return clob;
    }
 
 
    @Override
    public final void close() throws Exception {
        blobs.forEach(JDBCUtils::safeFree);
        clobs.forEach(JDBCUtils::safeFree);
    }
}

This simple class has some nice treats:

  • It’s AutoCloseable, so you can free your lobs with the try-with-resources statement
  • It abstracts over the creation of LOBs across SQL dialects. No need to remember the Oracle way

To use this class, simply write something like the following:

1
2
3
4
5
6
7
8
9
try (
    LOB lob = new LOB(connection);
    PreparedStatement stmt = connection.prepareStatement(
        "insert into lobs (id, lob) values (?, ?)")
) {
    stmt.setInt(1, 1);
    stmt.setClob(2, lob.clob("abc"));
    stmt.executeUpdate();
}

That’s it! No need to keep references to the lob, safely freeing it if it’s not null, correctly recovering from exceptions, etc. Just put the LOB container in the try-with-resources statement, along with the PreparedStatement and done.

If you’re interested in why you have to call Clob.free() or Blob.free() in the first place, read our article about it. It’ll spare you one or twoOutOfMemoryErrors

分享到:
评论

相关推荐

    Oracle clob和blob在jdbc的应用

    在Java编程中,Oracle数据库提供了对大型对象(Large Object)的支持,包括CLOB(Character Large Object)和BLOB(Binary Large Object)。这两种类型用于存储大量的文本数据(CLOB)和二进制数据(BLOB),例如...

    CLOB、BLOB___CLOB与BLOB的区别

    CLOB、BLOB 与 CLOB 与 BLOB 的区别 CLOB(Character Large OBject)是一种数据库类型,用于存储大型字符对象。它是 SQL 类型,用于存储字符大对象(Character Large Object),将字符大对象存储为数据库表某一行中...

    DELPHI的CLOB和BLOB存取处理.zip_DELPHI的CLOB和BLOB存取处理_hallzgz

    在数据库编程领域,CLOB(Character Large Object)和BLOB(Binary Large Object)是用于存储大量文本和二进制数据的数据类型。在DELPHI中,处理这些大型对象时需要特殊的技术和策略。本篇文章将深入探讨DELPHI中...

    Mybatis 处理 CLOB、BLOB 类型数据

    Mybatis 处理 CLOB、BLOB 类型数据 MyBatis 处理 CLOB、BLOB 类型数据是指在使用 MyBatis 框架时,如何正确地处理大字段类型的数据。CLOB(Character Large OBject)和 BLOB(Binary Large OBject)都是大字段类型...

    Oracle导出Clob,Blob工具

    Oracle数据库在处理大对象(LOB)类型,如Clob(Character Large Object)和Blob(Binary Large Object)时,有时需要专门的工具来进行高效且安全的数据导出。这些字段通常存储大量的文本或二进制数据,比如长篇文档...

    java中(注解符)处理Clob(blob)类型

    Java 中处理 Clob 和 Blob 类型的注解配置 Java 中处理 Clob 和 Blob 类型的注解配置是一种常见的处理大规模数据的方法。Clob(Character Large OBject)类型和 Blob(Binary Large OBject)类型是数据库中两种常用...

    spring2通过jdbc的方式读取、更新数据库的clob或者blob类型的数据

    总的来说,Spring通过JDBC提供了一套完善的接口和工具类来处理CLOB和BLOB,使得开发者无需直接与JDBC API打交道,降低了数据库操作的复杂度。在实际项目中,根据业务需求,合理运用这些功能可以极大地提高开发效率和...

    修改clob blob 字段

    修改clob blob 字段 修改clob blob 字段 修改clob blob 字段

    关于在Hibernate中对于Clob,Blob字段的处理方法

    本篇将详细介绍在Hibernate中如何处理Clob和Blob字段,以及使用特定Oracle JDBC驱动程序的优势。 首先,Hibernate通过`org.hibernate.type`包中的`ClobType`和`BlobType`来映射Clob和Blob。在实体类中,你需要定义...

    oracle(blob转换为clob)

    在示例代码中,`blob_to_clob`函数首先创建了一个临时的CLOB变量`v_clob`。然后,通过`DBMS_LOB.GETLENGTH`获取BLOB的总长度,计算出需要处理的次数`tmp_num`,确保每次处理不超过32767个字符,这是`VARCHAR2`类型的...

    clob-blob.rar_blob and clob_clob_java CLOB_java oracle cl_oracle

    对于CLOB和BLOB字段,Oracle JDBC驱动提供了`oracle.jdbc.driver.OracleClob`和`oracle.jdbc.driver.OracleBlob`类,它们实现了Java的标准接口`java.sql.Clob`和`java.sql.Blob`。这两个接口提供了读取和写入大数据...

    JDBC中操作Blob、Clob等对象

    ### JDBC中操作Blob、Clob等对象 #### 一、简介 在JDBC(Java Database Connectivity)编程中,Blob和Clob是非常重要的数据类型。Blob(Binary Large Object)主要用于存储二进制大对象,如图像、音频文件等;而Clob...

    mysql和Oracle在对clob和blob字段的处理

    本文将深入探讨这两个数据库系统如何处理`CLOB`(Character Large Object)和`BLOB`(Binary Large Object)字段,这两种字段类型主要用于存储大量文本或二进制数据。 `CLOB`用于存储非结构化的字符数据,如长篇...

    Oracle导出Clob,Blob工具版本2

    Oracle数据库在存储大对象(BLOB和CLOB)时提供了强大的功能,但处理这些类型的数据进行导入导出可能会带来挑战。"Oracle导出Clob,Blob工具版本2" 是一个专为解决这个问题而设计的应用程序,它改进了对CLOB...

    Hibernate操作Oarcle中Clob、Blob字段小结

    在Java的持久化框架Hibernate中,处理大数据类型如Oracle数据库中的Clob(Character Large Object)和Blob(Binary Large Object)字段是一项重要的任务。Clob通常用于存储大量的文本数据,而Blob则适用于二进制...

    图片存入Oracle中,用clob和blob两种方式

    在数据库管理中,存储非结构化数据如图片、音频或视频文件时,通常会使用`CLOB`(Character Large Object)和`BLOB`(Binary Large Object)这两种数据类型。Oracle数据库系统支持这两种数据类型,用于存储大量文本...

    BLOB和CLOB的区别以及在ORALCE中的插入和查询操作

    在数据库管理领域,BLOB(Binary Large Object)和CLOB(Character Large Object)是两种用于存储大量非结构化数据的数据类型。它们都是Oracle数据库中重要的字段类型,用于处理大数据量的文本或二进制文件。了解...

    oracle中的Blob和Clob区别

    ### Oracle中的BLOB和CLOB的区别 在Oracle数据库中,`BLOB`(Binary Large Object)和`CLOB`(Character Large Object)是用于存储大量数据的两种特殊数据类型。这两种类型都属于`LOB`(Large Object)类别,主要...

Global site tag (gtag.js) - Google Analytics