Ryan's Java world!

          something about Java and opensource!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks

          在前面一篇文章中,

          http://www.aygfsteel.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?

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

          Feedback

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

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

          與netbeans的集成 的教程點擊這里
          http://blog.matrix.org.cn/page/icess?entry=derby_tutorial_with_netbeans  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          JSF中文技術文摘
          主站蜘蛛池模板: 陵川县| 龙泉市| 德昌县| 河源市| 乌拉特中旗| 秭归县| 元朗区| 贺兰县| 额济纳旗| 常熟市| 南昌县| 大名县| 山阴县| 镇坪县| 双峰县| 广德县| 三门峡市| 昌黎县| 聊城市| 渝北区| 长汀县| 张掖市| 阳朔县| 龙井市| 竹北市| 深州市| 汝州市| 江油市| 简阳市| 榆中县| 拜泉县| 盐津县| 镇远县| 桐庐县| 桂林市| 瓮安县| 清河县| 柞水县| 大英县| 紫阳县| 马尔康县|