在前面一篇文章中,
http://www.tkk7.com/51AOP/archive/2006/04/13/40975.html
簡單介紹了 derby 的使用,有朋友問我derby 只不支持? blob和clob, 我到官方網站看看,在參考文檔中給出來一個例子,整理一下 那來做個例子,?下面來看看如何在derby中操作 clob的例子吧.
在前面代碼中加入如下一個方法.?
????
public?
Connection?getConnection
()?{
??????
return?
dbConnection;
????
}
創建一個測試clob的表 sql語句如下:
private?static?final?
String?strCreateTestClobTeble?=?
????
"CREATE?TABLE?APP.documents?(id?INT,?text?CLOB(64?K))"
;
???
測試代碼如下:
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
()
;
????????
//?測試clob?數據
????????
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
()
;
????
}
可見 在derby中操作 clob數據和其他數據庫是一樣的,blob也是一樣的 這里就不在測試了.
?
其實 derby的使用和其他的數據庫(如: mysql)使用基本上是一樣的, 支持標準的sql 語句和jdbc. 唯一不同的就是要 編程知道數據保存的位置,和 編程控制數據庫的開啟和關閉.
該測試的完整代碼請點擊: http://icess.tengyi.cn/opensource/Derby/src/testcolb.html?