原文:http://www.iteye.com/topic/65796
作者:jelly
LONG:
可变长的字符串
数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列
LONG RAW:
可变长二进制
数据,最长2G
CLOB
:
字符大对象Clob
用来存储单字节的字符
数据
NCLOB:
用来存储多字节的字符
数据
BLOB:
用于存储二进制
数据
BFILE:
存储在文件中的二进制
数据,这个文件中的数据只能被只读访。但该文件不包含在数据库内。
bfile字段实际的文件存储在文件系统中,字段中存储的是文件定位指针.bfile对oracle
来说是只读的,也不参与事务性控制和数据恢复.
CLOB
,NCLOB,BLOB都是内部的LOB(Large Object)
类型,最长4G,没有LONG只能有一列的限制
要保存图片、文本文件、Word文件各自最好用哪种数据类型?
--BLOB最好,LONG RAW也不错,但Long是oracle
将要废弃的类型,因此建议用BLOB。
二、操作
1、 get
CLOB
java 代码
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(
false
);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(
"select CLOBATTR from TESTCLOB where ID=1"
);
if
(rs.next())
{
java.sql.Clob
clob
= rs.getClob(
"CLOBATTR"
);
Reader inStream = clob
.getCharacterStream();
char
[] c =
new
char
[(
int
) clob
.length()];
inStream.read(c);
data =
new
String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();
BLOB
java 代码
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(
false
);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(
"select BLOBATTR from TESTBLOB where ID=1"
);
if
(rs.next())
{
java.sql.Blob blob = rs.getBlob(
"BLOBATTR"
);
InputStream inStream = blob.getBinaryStream();
data =
new
byte
[input.available()];
inStream.read(data);
inStream.close();
}
inStream.close();
con.commit();
con.close();
2、 put
CLOB
java 代码
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(
false
);
Statement st = con.createStatement();
st.executeUpdate(
"insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "
thename
", empty_clob())"
);
ResultSet rs = st.executeQuery(
"select CLOBATTR from TESTCLOB where ID=1 for update"
);
if
(rs.next())
{
oracle
.sql.CLOB
clob
= (oracle
.sql.CLOB
) rs.getClob(
"CLOBATTR"
);
Writer outStream = clob
.getCharacterOutputStream();
char
[] c = data.toCharArray();
outStream.write(c,
0
, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
BLOB
java 代码
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(
false
);
Statement st = con.createStatement();
st.executeUpdate(
"insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "
thename
", empty_blob())"
);
ResultSet rs = st.executeQuery(
"select BLOBATTR from TESTBLOB where ID=1 for update"
);
if
(rs.next())
{
oracle
.sql.BLOB blob = (oracle
.sql.BLOB) rs.getBlob(
"BLOBATTR"
);
OutputStream outStream = blob.getBinaryOutputStream();
outStream.write(data,
0
, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();