在前面一篇文章中,
http://www.tkk7.com/51AOP/archive/2006/04/13/40975.html
簡(jiǎn)單介紹了 derby 的使用,有朋友問(wèn)我derby 只不支持? blob和clob, 我到官方網(wǎng)站看看,在參考文檔中給出來(lái)一個(gè)例子,整理一下 那來(lái)做個(gè)例子,?下面來(lái)看看如何在derby中操作 clob的例子吧.
在前面代碼中加入如下一個(gè)方法.?
????
public?
Connection?getConnection
()?{
??????
return?
dbConnection;
????
}
創(chuàng)建一個(gè)測(cè)試clob的表 sql語(yǔ)句如下:
private?static?final?
String?strCreateTestClobTeble?=?
????
"CREATE?TABLE?APP.documents?(id?INT,?text?CLOB(64?K))"
;
???
測(cè)試代碼如下:
public?static?void?
main
(
String
[]?
args
)?{
??????
TestShutdown?db?=?
new?
TestShutdown
()
;
????????
System.out.println
(
db.getDatabaseLocation
())
;
????????
System.out.println
(
db.getDatabaseUrl
())
;
????????
long?
startTime?=?System.currentTimeMillis
()
;
????????
System.out.println
(
startTime
)
;
????????
db.connect
()
;
????????
//?測(cè)試clob?數(shù)據(jù)
????????
File?file?=?
new?
File
(
"test.txt"
)
;
????
int?
fileLenth?=?
(
int
)
file.length
()
;
????
????
try?
{
??????
//?first?,create?an?inputStream
??????
InputStream?is?=?
new?
FileInputStream
(
file
)
;
??????
PreparedStatement?ps?=?db.getConnection
()
.prepareStatement
(
"INSERT?INTO?APP.documents?VALUES?(?,??)"
,Statement.RETURN_GENERATED_KEYS
)
;
??????
ps.setInt
(
1
,?
1477
)
;
??????
//?-?set?the?value?of?the?input?parameter?to?the?input?stream
??????
ps.setAsciiStream
(
2
,?is,?fileLenth
)
;
??????
ps.executeUpdate
()
;
??????
db.getConnection
()
.commit
()
;
??????
??????
System.out.println
(
"write?clob?data?over!?\n?and?now?read?it?out."
)
;
??????
//---?reading?the?columns
??????
ResultSet?rs?=?db.getConnection
()
.createStatement
()
.executeQuery
(
"SELECT?text?FROM?APP.documents?WHERE?id?=?1477"
)
;
??????
while
(
rs.next
())?{
????????
Clob?clob?=?rs.getClob
(
1
)
;
????????
System.out.println
(
clob.toString
())
;
????????
InputStream?ip?=?rs.getAsciiStream
(
1
)
;
????????
int?
c?=?ip.read
()
;
????????
while
(
c?>?
0
)?{
??????????
System.out.print
((
char
)
c
)
;
??????????
c?=?ip.read
()
;
????????
}
??????
}
????
}?
catch?
(
FileNotFoundException?e
)?{
??????
//?TODO?Auto-generated?catch?block
??????
e.printStackTrace
()
;
????
}?
catch?
(
SQLException?e
)?{
??????
//?TODO?Auto-generated?catch?block
??????
e.printStackTrace
()
;
????
}?
catch
(
IOException?e
)?{
??????
????
}
????????
db.disconnect
()
;
????
}
可見(jiàn) 在derby中操作 clob數(shù)據(jù)和其他數(shù)據(jù)庫(kù)是一樣的,blob也是一樣的 這里就不在測(cè)試了.
?
其實(shí) derby的使用和其他的數(shù)據(jù)庫(kù)(如: mysql)使用基本上是一樣的, 支持標(biāo)準(zhǔn)的sql 語(yǔ)句和jdbc. 唯一不同的就是要 編程知道數(shù)據(jù)保存的位置,和 編程控制數(shù)據(jù)庫(kù)的開(kāi)啟和關(guān)閉.
該測(cè)試的完整代碼請(qǐng)點(diǎn)擊: http://icess.tengyi.cn/opensource/Derby/src/testcolb.html?