Ryan's Java world!

          something about Java and opensource!

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks

          在前面一篇文章中,

          http://www.aygfsteel.com/51AOP/archive/2006/04/13/40975.html

          簡(jiǎn)單介紹了 derby 的使用,有朋友問我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 () ;
          ???? }

          可見 在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ù)的開啟和關(guān)閉.

          該測(cè)試的完整代碼請(qǐng)點(diǎn)擊: http://icess.tengyi.cn/opensource/Derby/src/testcolb.html?

          posted on 2006-04-14 15:23 冰雨 閱讀(3520) 評(píng)論(2)  編輯  收藏

          Feedback

          # re: 在derby(Java DB) 中操作 clob 和 blob 2006-04-14 17:43 Jonney
          冰雨知道Derby有什么圖形界面的工具可以瀏覽數(shù)據(jù)和創(chuàng)建Table嗎?
          我覺得用SQL語(yǔ)句創(chuàng)建Table挺麻煩的。  回復(fù)  更多評(píng)論
            

          # re: 在derby(Java DB) 中操作 clob 和 blob 2006-04-14 19:31 冰雨
          derby 與 netbean 集成的很好
          與 eclipse 集成的也不錯(cuò) 有幾個(gè)eclipse的插件可以用
          在 derby 主頁(yè)上下載

          與netbeans的集成 的教程點(diǎn)擊這里
          http://blog.matrix.org.cn/page/icess?entry=derby_tutorial_with_netbeans  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          JSF中文技術(shù)文摘
          主站蜘蛛池模板: 五原县| 屏边| 甘谷县| 枞阳县| 论坛| 涞水县| 浦东新区| 廉江市| 钦州市| 连江县| 大同县| 岐山县| 上犹县| 呼和浩特市| 滦南县| 个旧市| 垣曲县| 苍梧县| 崇信县| 吴江市| 德阳市| 昌平区| 新乡市| 呈贡县| 商洛市| 江西省| 万源市| 留坝县| 东乡族自治县| 澄迈县| 巫溪县| 县级市| 石泉县| 扶风县| 北海市| 余江县| 门头沟区| 江城| 上林县| 古田县| 甘孜|