Display Mysql "Blob" data using servlet


 

Display Mysql "Blob" data using servlet

In this section, we will discuss about how to display a blob data stored in database table.

In this section, we will discuss about how to display a blob data stored in database table.

Display Mysql  "Blob" data using servlet

In this section, we will discuss about how to display a blob data stored in database table. A Blob stores a binary large object in the database table's row. Blob object contains a logical pointer which points to the Blob data, data is not directly stored in the row of the database table. Blob object  is valid for the duration of the transaction. The "getBlob()" and  "setBlob()" method of ResultSet ,CallableStatement, and PreparedStatement interface , is used for accessing Blob value. Blob was added to java in jdk1.2 .

The Method defined in " java.sql.Blob " interface is as follows--

Return Type    Method                                         Description

InputStream        getBinaryStream()                       Retrieves the blob value designated
                                                                                 by this blob value as a stream

byte[]                 getBytes(long pos, int length)         Retrieve all or part of the blob value that
                                                                                 this blob represents as an array of bytes.

long                    length()                                          Returns the no of bytes in the Blob
                                                                                 value designated by this Blob object

OutputStream     setBinaryStream(long pos)           Retrieves a stream that can be used
                                                                                 to write to the Blob value 

int                       setBytes(long pos, byte[] bytes)    Write the given array of bytes to the Blob
                                                                                 value that this Blob object represent
                                                                                 starting at  position pos, and returns the no
                                                                                 of bytes written 

void                    truncate(long len)                            Truncates the blob value that this blob
                                                                                  object represents to be len bytes in len

 

Blob data type can further be classified into four types--

  • TINYBLOB: The maximum length is 255 characters (8 bits)
  • BLOB: The maximum length is 16,535 characters (16 bits)
  • MEDIUMBLOB: The maximum length is 16,777,216 characters (24 bits)
  • LONGBLOB: The maximum length is 4,294,967,295 characters (32 bits).

displayblob.java

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class displayblob extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
    Blob image = null;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    ServletOutputStream out = response.getOutputStream();
    try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection
("jdbc:mysql://192.168.10.13:3306/ankdb","root","root");
      stmt = con.createStatement();
      rs = stmt.executeQuery("select image from inimage where  id = '1'");
      if (rs.next()) {
        image = rs.getBlob(1);
      } else {
        response.setContentType("text/html");
        out.println("<html><head><title>Display Blob Example</title></head>";
out.print("<body><h4><font color='red'>image not found for given 
   id</font></h4></body></html>";
        return;
      }
      response.setContentType("image/gif");
      InputStream in = image.getBinaryStream();
      int length = (int) image.length();
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];
      while ((length = in.read(buffer)) != -1) {
        out.write(buffer, 0, length);
      }
      in.close();
      out.flush();
    } catch (Exception e) {
      response.setContentType("text/html");
      out.println("<html><head><title>Unable To Display image</title></head>");
      out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() + "</font></h4></body></html>");
      return;
    } finally {
      try {
        rs.close();
        stmt.close();
        con.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

OUTPUT

Download Source Code

Ads