devxlogo

Read an Image from a Database Using the getBinaryStream Method

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());}...
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist