Showing posts with label upload display image from oracle database in jdeveloper 11g. Show all posts
Showing posts with label upload display image from oracle database in jdeveloper 11g. Show all posts

Saturday, 17 December 2011

Upload and Display Image from Database Blob Column in Oracle ADF Jdeveloper 11g R2


1- MAKE NEW SERVLET



COPY TEXT FROM BELOW TO NEW SERVLET.
DEFINE IMAGE SOURCE WITH SERVLET NAME AND PARAMETER VALUE.


DISPLAY MAGE SERVLET CODING

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import java.sql.Blob;
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.sql.DataSource;


public class Servlet1 extends HttpServlet {
    private static final String CONTENT_TYPE = "image/gif; charset=utf-8";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
       

        response.setContentType(CONTENT_TYPE);
        String imageId = request.getParameter("id");
        OutputStream os = response.getOutputStream();
        Connection conn = null;
        try {
            Context ctx = new InitialContext();           
            conn = getOracleConnection();
            PreparedStatement statement =
                conn.prepareStatement("SELECT employee_id,pic " +
                                      "FROM employees " +
                                      "WHERE employee_id = ?");
            statement.setInt(1, new Integer(imageId));
            ResultSet rs = statement.executeQuery();

            if (rs.next()) {
                Blob blob = rs.getBlob("PIC");               
                BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
                int b;
                byte[] buffer = new byte[10240];
                while ((b = in.read(buffer, 0, 10240)) != -1) {
                    os.write(buffer, 0, b);
                }
                os.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("SQLException error");
            }
        }
    }
    public static Connection getOracleConnection() throws Exception {
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:orcl";
      String username = "hr";
      String password = "hr";

      Class.forName(driver); // load Oracle driver
      Connection conn = DriverManager.getConnection(url, username, password);
      return conn;
    }

}

UPLOAD IMAGE CODING

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.sql.SQLException;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;

import oracle.jbo.Row;
import oracle.jbo.domain.BlobDomain;

import org.apache.myfaces.trinidad.model.UploadedFile;


public class UploadBean {
    public UploadBean() {
        super();
    }

    private UploadedFile _file;

    public UploadedFile getFile() {
        return _file;
    }

    public void setFile(UploadedFile file) {
        _file = file;
    }

    public String uploadImage() {

        UploadedFile myfile = (UploadedFile)this.getFile();
       
        BindingContext bindingctx = BindingContext.getCurrent();
        BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
        DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
        DCIteratorBinding iter = bindingsImpl.findIteratorBinding("ABCView1Iterator");
       
        Row row = iter.getCurrentRow();
        row.setAttribute("Pic", createBlobDomain(myfile));
   
     return null;
    }

    private BlobDomain createBlobDomain(UploadedFile file) {

        InputStream in = null;
        BlobDomain blobDomain = null;
        OutputStream out = null;

        try {
            in = file.getInputStream();

            blobDomain = new BlobDomain();
            out = blobDomain.getBinaryOutputStream();
            byte[] buffer = new byte[8192];
            int bytesRead = 0;

            while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            in.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.fillInStackTrace();
        }

        return blobDomain;
    }

}