Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Jun 10, 2008

Read an Image from a Database Using the getBinaryStream Method

This following code shows you how to extract an image (in this example, named foto_9738533.gif) from a database using the getBinaryStream method:

try{
   sql="SELECT Foto FROM Table1 WHERE Nr_cont=9738533";
   rs=st.executeQuery(sql);
   if(rs.next())
      {
      is=rs.getBinaryStream("Foto");
      try{
         java.io.FileOutputStream out= new java.io.FileOutputStream 
            ("C:\\sgbd\\foto_9738533.gif"); 
         byte[] bf = new byte[6144]; 
         int foto_bytes=0;  
         while ((foto_bytes = is.read(bf))!=-1) 
                 { 
                 out.write(bf, 0, foto_bytes);
                 } 
         out.close();
         }catch(java.io.IOException e)
            {System.out.println(e.getMessage());}
         }
         }catch(SQLException e)
            {System.out.println(e.getMessage());}
...
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap