Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統計

          留言簿(23)

          積分與排名

          優秀學習網站

          友情連接

          閱讀排行榜

          評論排行榜

          hibernate中映射blob數據類型的一個例子 (zhuan)

           1java 代碼
           2public class User  implements java.io.Serializable {    
           3   
           4   
           5    // Fields        
           6   
           7     private long id;    
           8     private String name;    
           9     private String email;    
          10     private String addr;    
          11     //定義Blob的pthto    
          12     private Blob photo;   
          13
          14xml 代碼
          15<hibernate-mapping>   
          16    <class name="org.tie.User" table="user" catalog="tie">   
          17        <id name="id" type="long">   
          18            <column name="id" />   
          19            <generator class="identity" />   
          20        </id>   
          21        <property name="name" type="string">   
          22            <column name="name" length="45" not-null="true" />   
          23        </property>   
          24        <property name="email" type="string">   
          25            <column name="email" length="45" />   
          26        </property>   
          27        <property name="addr" type="string">   
          28            <column name="addr" length="45" />   
          29        </property>   
          30        <!-- 映射blob類型 -->   
          31        <property name="photo" type="blob">   
          32            <column name="photo" />   
          33        </property>   
          34    </class>   
          35</hibernate-mapping>   
          36
          37兩個測試方法:
          38
          39java 代碼
          40public void testCreate(){    
          41            
          42        User user = new User();    
          43        user.setName("linweiyang");    
          44        user.setAddr("beijing");    
          45        user.setEmail("linweiyang@163.com");    
          46        Blob photo = null;    
          47        
          48        try {    
          49            //將圖片讀進輸入流    
          50            FileInputStream fis = new FileInputStream("c:\\a.jpg");    
          51            //轉成Blob類型    
          52            photo = Hibernate.createBlob(fis);    
          53                
          54        }
           catch (FileNotFoundException e) {    
          55            e.printStackTrace();    
          56        }
           catch (IOException e) {    
          57            e.printStackTrace();    
          58        }
              
          59                
          60        user.setPhoto(photo);    
          61            
          62        Session session = factory.openSession();    
          63        Transaction tr = session.beginTransaction();    
          64        session.save(user);    
          65        tr.commit();    
          66        session.close();    
          67   
          68    }
              
          69        
          70    public void testRerieve(){    
          71            
          72        Session session = factory.openSession();    
          73        User user = (User)session.load(User.classnew Long(3));    
          74        try {    
          75            //從數據庫中要讀取出來    
          76            InputStream is = user.getPhoto().getBinaryStream();    
          77            //在把寫到一個圖片格式的文件里    
          78            FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");    
          79                
          80            byte[] buffer = new byte[1024];    
          81            int len = 0;    
          82            //從數據庫中讀取到指定的字節數組中    
          83            while((len = is.read(buffer) )!= -1){    
          84                //從指定的數組中讀取,然后輸出來,所以這里buffer好象是連接inputStream和outputStream的一個東西    
          85                fos.write(buffer,0,len);    
          86            }
              
          87        }
           catch (FileNotFoundException e) {    
          88            e.printStackTrace();    
          89        }
           catch (SQLException e) {    
          90            e.printStackTrace();    
          91        }
           catch (IOException  e){    
          92            e.printStackTrace();    
          93        }
                         
          94        session.close();    
          95    }
             

          這么理解輸入輸出流

          讀入流,自然要有讀入的源頭,

          輸出也要輸出到某個地方,輸出一般是先要輸讀入,

          這里連接輸入和輸出的是一個在內存中的字節數組buffer.這樣從數據庫中讀到這個數組里,輸出流在從這個數組中輸出到特定的文件格式里.

          posted on 2007-12-17 16:25 都市淘沙者 閱讀(2018) 評論(1)  編輯  收藏 所屬分類: Hibernate/ORM

          評論

          # Podbor Klyuchevyh Slov 2009-05-18 10:34 Podbor Klyuchevyh Slov

          Badly need your help. A smiling face is half the meal.
          I am from Saudi and , too, and now am writing in English, please tell me right I wrote the following sentence: "Algorithm analysis, web community relationship analysis, seo practices and techniques, industry news, etc.Miami seo by unlimitedwebdesigns, south florida seo."

          Thanks :-D. Dempsey.  回復  更多評論   

          主站蜘蛛池模板: 云和县| 乌兰县| 靖边县| 明光市| 渭源县| 桦甸市| 荣昌县| 绥棱县| 容城县| 锡林郭勒盟| 乐平市| 禹州市| 广宁县| 涟源市| 隆回县| 鄂伦春自治旗| 湘西| 香河县| 泽普县| 宁强县| 福贡县| 井冈山市| 长泰县| 西青区| 罗定市| 兰西县| 称多县| 庆元县| 汉中市| 铜陵市| 犍为县| 特克斯县| 金川县| 陵水| 台州市| 衡阳市| 水城县| 资阳市| 潼关县| 宁河县| 南和县|