0

how to retrieve image from oracle and display in java frame. Please send me a sample program I'm using oracle 10G express edition.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

@SuppressWarnings("serial")
public class Search extends JFrame implements ActionListener {

//Initializing Components
    private JLabel lb, lb1, lb2, lb3, lb4, lb5, lb6;
    private JTextField tf1, tf2, tf3, tf4, tf5;
    private byte s4;
    private JButton btn;
    private Connection con;

    Search() {//Creating Constructor for initializing JFrame components
        //Providing Title
        super("Fetching Student Information");
        lb5 = new JLabel("Enter the Employee id:");
        lb5.setBounds(20, 20, 100, 20);
        tf5 = new JTextField(20);
        tf5.setBounds(130, 20, 200, 20);
        btn = new JButton("Submit");
        btn.setBounds(50, 50, 100, 20);
        btn.addActionListener(this);
        lb = new JLabel("Fetching Employee Information From Database");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.red);
        lb.setFont(new Font("Serif", Font.BOLD, 20));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        lb1 = new JLabel("EmployeeName:");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);
        lb2 = new JLabel("Gender:");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);
        lb3 = new JLabel("DOB:");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextField(50);
        tf3.setBounds(130, 180, 200, 20);
        lb4 = new JLabel("DOJ:");
        lb4.setBounds(20, 210, 100, 20);
        tf4 = new JTextField(50);
        tf4.setBounds(130, 210, 100, 20);
        lb6 = new JLabel("Photo:");
        lb6.setBounds(550, 10, 100, 100);
        setLayout(null);
        setVisible(true);
        //Add components to the JFrame
        add(lb5);
        add(tf5);
        add(btn);
        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);
        add(lb4);
        add(tf4);
        add(lb6);
        //Set TextField Editable False
        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);
        tf4.setEditable(false);
    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records
        try {
            String str = tf5.getText();
            String url = "jdbc:oracle:thin:@localhost:1521:XE";
            String u = "ems2";
            String p = "ems2";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(url, u, p);
            PreparedStatement st = con.prepareStatement("select * from employee where emp_id=?",);
            st.setString(1, str);
            //Excuting Query
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                String s = rs.getString("employeename");
                String s1 = rs.getString("dob");
                String s2 = rs.getString("gender");
                String s3 = rs.getString("doj");
                //Sets Records in TextFields.
                tf1.setText(s);
                tf2.setText(s2);
                tf3.setText(s1);
                tf4.setText(s3);
            } else {
                JOptionPane.showMessageDialog(null, "please check your employeeID");
            }
            PreparedStatement ps1 = con.prepareStatement("select * from photo where photoid =?");
            ResultSet rs1 = ps1.executeQuery();
            while (rs.next()) {//now on 1st row  
                Blob b = rs.getBlob(2);//2 means 2nd column data  
                byte barr[] = b.getBytes(1, (int) b.length());//1 means first image
            }
            //Create Exception Handler
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

//Running Constructor
    public static void main(String args[]) {
        new Search();
    }
}
12
  • Hi this is my program i have to bring the photo the in my gui Commented Mar 13, 2014 at 5:18
  • But the issue is i have stored the photo in another table named photo so i tried to use the prepared statement twice in this program Commented Mar 13, 2014 at 5:19
  • It's better. I'm removing my downvote. Commented Mar 13, 2014 at 5:19
  • Your question is too wide. How the image is stored in oracle db? blob? which table/field? don't expect working code. Describe specific problem you have instead. Commented Mar 13, 2014 at 5:21
  • i want to know how to execute multiple select statements of different table from the same jdbc program. Thanks you very much :) Commented Mar 13, 2014 at 5:21

1 Answer 1

2

Call a query to retrieve image and use something like this.

byte[] imgData = null;
 if (rs.next ())
 {
   Blob img  = rs.getBlob(1);
   imgData = img.getBytes(1,(int)img.length());
   BufferedImage image = ImageIO.read(new ByteArrayInputStream(imgData));
   yourJLabelInstance.setIcon(new ImageIcon(image));
 }

The yourJLabelInstance is a JLabel created and added to your frame.

Sign up to request clarification or add additional context in comments.

8 Comments

What happens when the image data is greater then Integer.MAX_VALUE?
You are right. It's not perfect:-) It's just an example rather than ready to work code. In fact it's hardly that image's size is bigger than MAX int.
Agreed, just pocking ;)
hi @StanislavL how to execute join in the place of select query
@prasanth I don't understand the question:(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.