Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to display BLOB image using <img> html tag in JSP

843840Dec 2 2008 — edited Dec 3 2008
I Have got the image from oracle database but want to display BLOB image using <IMG src="" > Html tags in JSP page . If it is possible than please give some ideas or
Send me sample codes for display image.

This code is ok and working no problem here Please send me code How to display using html tag from oracle in JSP page.
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %>
<%@ page import="javax.swing.ImageIcon;" %>

      
      <%
        out.print("hiiiiiii") ; 
            
            // declare a connection by using Connection interface 
            Connection connection = null;

            /* Create string of connection url within specified format with machine 
			name, port number and database name. Here machine name id localhost 
			and database name is student. */

            String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";

            /*declare a resultSet that works as a table resulted by execute a specified 
			sql query. */
            ResultSet rs = null;

            // Declare statement.
            PreparedStatement psmnt = null;
           
		    // declare InputStream object to store binary stream of given image.
			InputStream sImage;

            try {

                // Load JDBC driver "com.mysql.jdbc.Driver"
                Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

				/* Create a connection by using getConnection() method that takes 
				parameters of string type connection url, user name and password to 
				connect to database. */
                connection = DriverManager.getConnection(connectionURL, "scott", "root");

				/* prepareStatement() is used for create statement object that is 
	           used for sending sql statements to the specified database. */
                psmnt = connection.prepareStatement("SELECT image FROM img WHERE id = ?");

                psmnt.setString(1, "10");
                rs = psmnt.executeQuery();
                if(rs.next()) {
                      byte[] bytearray = new byte[1048576];
                      int size=0;
                      sImage = rs.getBinaryStream(1); 
                      
                    //response.reset();
                      response.setContentType("image/jpeg");

                      while((size=sImage.read(bytearray))!= -1 ){
    
            response.getOutputStream().write(bytearray,0,size);
                
                                     
				      }
			    }
		    }
            catch(Exception ex){
				out.println("error :"+ex);
            }
	
           finally {
                // close all the connections.
                rs.close();
                psmnt.close();
                connection.close();
           }
           		
     %>
	
Thanks .
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2008
Added on Dec 2 2008
5 comments
6,475 views