As we know storing image in DB is bad practice, but it's helpful to store small images like thumbnails in DB for small applications to access it quickly.
This mainly helps in reducing development time if the application already making use of DB for some other operation, instead of reinventing the wheel to store these images on some directory and maintaining the same.
So following is a simple solution, which fetches images stored in MySQL DB as BLOB data type and displays the same on a web page implemented in JSP.
Following is the table structure/query I used for this application and it's assumed that data already exists in the table.
CREATE TABLE table_images(image_name VARCHAR(50),image_type VARCHAR(50),image BLOB);
This application requires MySQL JDBC driver(mysql-connector-java.jar) to be in application classpath.
Example code for Sample.jsp -
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<% Blob image = null;
Connection con = null;
byte[ ] imgData = null ;
Statement stmt = null;
ResultSet rs = null;
String dbname="testdb"; //Name of DB where the table table_images exists.
String dbusername="test"; // DB username to connect to testdb.
String dbuserpassword="password"; // DB password to connect to testdb.
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://loc alhost:3306/"+dbname,dbusername,dbuserpassword);
stmt = con.createStatement();
String name=request.getParameter("name");
String type="";
name=name.substring(0,name.indexOf(".")) ;
rs = stmt.executeQuery("select image,image_type from table_images where image_name = '"+name+"'");
if (rs.next())
{
image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
type=rs.getString(2);
}
else
{
out.println("Unable to find image -- "+name);
return;
}
response.setContentType("image/"+type);
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
out.println("Exception occured ." + e.getMessage());
return;
}
finally
{
try
{
rs.close();
stmt.close();
con.close();
}
catch (SQLException e)
{
out.println(e.getMessage());
}
} %>
As always this is one of the several solution .
This mainly helps in reducing development time if the application already making use of DB for some other operation, instead of reinventing the wheel to store these images on some directory and maintaining the same.
So following is a simple solution, which fetches images stored in MySQL DB as BLOB data type and displays the same on a web page implemented in JSP.
Following is the table structure/query I used for this application and it's assumed that data already exists in the table.
CREATE TABLE table_images(image_name VARCHAR(50),image_type VARCHAR(50),image BLOB);
This application requires MySQL JDBC driver(mysql-connector-java.jar) to be in application classpath.
Example code for Sample.jsp -
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<% Blob image = null;
Connection con = null;
byte[ ] imgData = null ;
Statement stmt = null;
ResultSet rs = null;
String dbname="testdb"; //Name of DB where the table table_images exists.
String dbusername="test"; // DB username to connect to testdb.
String dbuserpassword="password"; // DB password to connect to testdb.
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://loc alhost:3306/"+dbname,dbusername,dbuserpassword);
stmt = con.createStatement();
String name=request.getParameter("name");
String type="";
name=name.substring(0,name.indexOf(".")) ;
rs = stmt.executeQuery("select image,image_type from table_images where image_name = '"+name+"'");
if (rs.next())
{
image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
type=rs.getString(2);
}
else
{
out.println("Unable to find image -- "+name);
return;
}
response.setContentType("image/"+type);
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
out.println("Exception occured ." + e.getMessage());
return;
}
finally
{
try
{
rs.close();
stmt.close();
con.close();
}
catch (SQLException e)
{
out.println(e.getMessage());
}
} %>
As always this is one of the several solution .
No comments:
Post a Comment