Java 将Clob类型转换为String

SQL CLOB 是内置类型,它将字符大对象存储为数据库表某一行中的一个列值,使用CHAR来存储数据,如XML文档。
如下是一个Clob转换为String的静态方法,可将其放在自己常用的工具类中,想直接用的话,自己稍作修改即可

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public
static
String
clobToStr
(Clob clob)
{
if
(clob ==
null
) {
return
""
;
}
StringBuffer
strClob
=
new
StringBuffer
();
String
str
=
""
;
Reader
read
=
null
;
try
{
reader = clob.getCharacterStream();
char
[] buffer =
new
char
[
];
int
length
=
;
while
(length = reader.read(buffer,
,
)) != -
) {
strClob.append(buffer,
, length);
}
}
catch
(SQLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
if
(reader !=
null
)
reader.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
str = strClob.toString();
return
str;
}

我在将数据导出成Excel时碰到的问题,需要导出的数据中有Clob格式只需将其Clob对象(若直接导出则显示的是地址)
这个工具挺好用的,放在这里,以后方便自己使用。
另:博客园的代码排版实在太丑了,以后还是在外面编辑好再粘贴进来比较好
2022.08.06迁移吐槽:太好笑了,现在你已经到github了,再也不用担心博客园的排版了!


Java 将Clob类型转换为String
https://zhouyinglin.top/2022/08/06/Java 将Clob类型转换为String/
作者
小周
发布于
2022年8月6日
许可协议