Search

Retrieve Image from database with Java

In this article, I am going to describe how to retrieve images from database (Oracle database) using an java.
Let’s see how to retrieve image from database.
Here is explanation.

Java Program


import java.sql.*;
import java.io.*;

public class RetrieveImage{

 static Connection connection = null;
 static CallableStatement pstat = null;
 static String connectionURL = null;

 public static void main(String[] args) {

  try{
  Class.forName("Oracle Connection Class");
  connection = DriverManager.getConnection("connection url","username", "password");

  PreparedStatement pstat = connection.prepareStatement("Select name,photo from image");

  ResultSet rs = pstat.executeQuery();
  rs.next(); // go to first row
  Blob b = rs.getBlob(2);
  byte[] bt = new byte[(int) b.length()];
  bt = b.getBytes(1, (int)b.length());

  FileOutputStream fout = new FileOutputStream("D:\\test.jpg");
  fout.write(bt);
  fout.close();

  System.out.println("Done");

  connection.close();

  }catch(Exception e){
   e.printStackTrace();
  }

 }

}


Resule: Done

No comments:

Post a Comment