ORACLE中的大對(duì)象:
LONG: 可變長(zhǎng)的字符串?dāng)?shù)據(jù),最長(zhǎng)2G,LONG具有VARCHAR2列的特性,可以存儲(chǔ)長(zhǎng)文本一個(gè)表中最多一個(gè)LONG列
LONG RAW: 可變長(zhǎng)二進(jìn)制數(shù)據(jù),最長(zhǎng)2G
CLOB: 字符大對(duì)象Clob 用來(lái)存儲(chǔ)單字節(jié)的字符數(shù)據(jù)
NCLOB: 用來(lái)存儲(chǔ)多字節(jié)的字符數(shù)據(jù)
BLOB: 用于存儲(chǔ)二進(jìn)制數(shù)據(jù)
BFILE: 存儲(chǔ)在文件中的二進(jìn)制數(shù)據(jù),這個(gè)文件中的數(shù)據(jù)只能被只讀訪。但該文件不包含在數(shù)據(jù)庫(kù)內(nèi)。bfile字段實(shí)際的文件存儲(chǔ)在文件系統(tǒng)中,字段中存儲(chǔ)的是文件定位指針.bfile對(duì)oracle來(lái)說(shuō)是只讀的,也不參與事務(wù)性控制和數(shù)據(jù)恢復(fù).
CLOB,NCLOB,BLOB都是內(nèi)部的LOB(Large Object)類(lèi)型,最長(zhǎng)4G,沒(méi)有LONG只能有一列的限制。
要保存圖片、文本文件、Word文件各自最好用哪種數(shù)據(jù)類(lèi)型?
--BLOB最好,LONG RAW也不錯(cuò),但Long是oracle將要廢棄的類(lèi)型,因此建議用BLOB。
對(duì)CLOB與BLOB對(duì)象的操作
1. 查詢(GET)
查詢CLOB
//獲得數(shù)據(jù)庫(kù)連接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
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是讀出并需要返回的數(shù)據(jù),類(lèi)型是String
data = new String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();
查詢BLOB
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
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是讀出并需要返回的數(shù)據(jù),類(lèi)型是byte[]
data = new byte[input.available()];
inStream.read(data);
inStream.close();
}
inStream.close();
con.commit();
con.close();
2. 插入(INSERT)
插入CLOB
//獲得數(shù)據(jù)庫(kù)連接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.CLOB
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");
Writer outStream = clob.getCharacterOutputStream();
//data是傳入的字符串,定義:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
插入BLOB
//獲得數(shù)據(jù)庫(kù)連接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是傳入的byte數(shù)組,定義:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
posted on 2010-09-29 08:19
三角形 閱讀(2466)
評(píng)論(0) 編輯 收藏