1

I am trying to fetch an image from the oracle 10g database in a jframe, but i am not able to do it. Its not showing anything in the frame, and there is no compilation error in the program, because, my program is runnig, and its showing an empty jframe. I am providing the detail code along with the database structure. Please help me finding the solution.

Create Table Structure in the database:

  create table abc
  ( 
    img blob
  );

This is the insert image code:

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class test1
{
  public static void main(String args[]) throws Exception
  {
     Connection conn;

     try
     {
       Class.forName("oracle.jdbc.driver.OracleDriver");
       conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
           if(conn != null)
           System.out.println("\n Connection created.");
           File imgfile = new File("D:\\ss.jpg");
           FileInputStream fin = new FileInputStream(imgfile);
           PreparedStatement pre = conn.prepareStatement("insert into abc values(?)");

           pre.setBinaryStream(1,fin,(byte)imgfile.length());

           pre.executeUpdate();
           conn.commit();
           System.out.println("Inserting Successfully!");
           pre.close();

        }
        catch(Exception e1) { System.out.println(e1);}
     }
 }

The code for retrieve image is:

  import java.awt.Graphics;
  import java.awt.Image;
  import java.sql.Blob;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;

  import javax.swing.JDesktopPane;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  public class RetrieveImage {  
  public static void main(String[] args) {  

  try{  
  Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection con =   DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott","tiger");

     PreparedStatement ps=con.prepareStatement("select * from abc");  
     ResultSet rs=ps.executeQuery();   
     rs.next();  
     Blob b=rs.getBlob(1);
     JLabel label = new JLabel();
     JFrame f = new JFrame();
     Image image = f.getToolkit().createImage(b.getBytes(1, (byte)b.length()));
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().add(new Pane(image)); 
     f.getContentPane().add(label);
     f.pack();
     f.setLocation(200,200);
     f.setVisible(true);
     con.close();  
   }
   catch (Exception e) {e.printStackTrace();  }  
 }  
}  
   @SuppressWarnings("serial")
   class Pane extends JDesktopPane
   {  
      private Image image;  
      public Pane(Image image) {  
      this.image = image;  
   }  
      public void paintComponent(Graphics g) {  
      super.paintComponent(g);  
      g.drawImage(image, 0, 0, this);  
  }  
}

The insert image is working fine, and it is inserting images into the table.. we cannot see the data(photo) in oracle directly, so i executed the command

     select rownum from abc;

It is showing the number of data in my table and i got to know that insert image is working.

So please find a proper solution in retrieving images from the database.

Shubhro
  • 73
  • 2
  • 11

0 Answers0