954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying Images( buffered data ) from the Database using Java


<%
try
{
javax.servlet.http.HttpServletResponse res=null;;
int returnValue = 0;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
InputStream in = null;
OutputStream os = null;
Blob blob = null;
String text;
text=request.getParameter("text");
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","veradis");
final String query = "SELECT image FROM tbl_image";
conn.setAutoCommit(false);
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
int i=1;
if(rs.next())
{
String len1 = rs.getString("image");
int len = len1.length();
byte [] b = new byte[len];
in = rs.getBinaryStream("image");
int index = in.read(b, 0, len);
OutputStream outImej = new FileOutputStream("C:/Documents and Settings/Tamil/Desktop/photo/img"+i+".JPG");
while (index != -1)
{
outImej.write(b, 0, index);
index = in.read(b, 0, len);
System.out.println("==========================");
System.out.println(index);
System.out.println(outImej);
System.out.println("==========================");
}
outImej.close();
i++;
}
else
{
returnValue = 1;
}
}
catch(Exception e)
{
out.println("SQLEXCEPTION : " +e);
}
%>

ttamilvanan81
Newbie Poster
24 posts since Apr 2007
Reputation Points: 30
Solved Threads: 0
 

What i did is: i have created a folder inside the same project, named it images, then i give a user an option to upload the image, when you upload the image, it takes the image and save it in the folder and take the image(file) name and store it into a database eg: my project name is support, inside support folder i have image folder which is @ c drive and the image name is lordkwena.gif, so in the in the database i am gonna have c:\\support\\image\\lordkwena.gif. so after a user has logged in it collects the information from the database including the image. i have created a method so that i will access it when ever i want. here is the method:

public void setImageI(String imageIc)
{

image=imageIc;
}
public String getImageI()
{
return image;
}
Hope you know what is happening here.
ResultSet rs = state.executeQuery(Query);
if(rs.next())
{

main.setName(rs.getString(4));
//plz note that main is: static className main = null; meaning i have decleared a static className main to create an instance for my class
}

so rs.getString(4) this will collect the infornmation form the database.
cos i was using java, i displayed the image on a label: lblImage.setIcon(new ImageIcon(imagename));

//declaring imagename
private String imagename="";
//Initialising imagename
imagename=main.getImageI();

Hope this helps, let me know if you have a problem.

lordkwena
Newbie Poster
2 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

Hai lordwena, Thanks for your reply.
I have already done this type of functionality, like store the images in image folder under the project root folder(ex: d:/Tomcat 5.0/webapps//), and the image name was stored into the database.

But i need the concept of Store the image content into the Database usiong BLOB Datatype, and whenever we need the image, we will get the image content from the database and display it in jsp page.

This type of functionality i need to use in my project, So please help me with this. In my project i am using MYSQL Database

Anyway Thanks alot for your reply.

Regards

Tamilvanan

ttamilvanan81
Newbie Poster
24 posts since Apr 2007
Reputation Points: 30
Solved Threads: 0
 

You are storing Images. Use BufferedInput/OutputStream. Also store the content type in a db column. And retrieve the same for setting the ContentType of jsp page. Do not do this
OutputStream outImej = new FileOutputStream("C:/Documents and Settings/Tamil/Desktop/photo/img"+i+".JPG");

Rather write the steam as byte array.

String query = "select image from images where id =1";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
rs.next();

byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType(rs.get("contentType"));
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}

response.flushBuffer();
sImage.close();

lookof2day
Junior Poster in Training
83 posts since Aug 2007
Reputation Points: 16
Solved Threads: 11
 

Image Slideshow <% String img=""; for(int i=0; i <% } %> <%=response.getOutputStream()%> Home

ttamilvanan81
Newbie Poster
24 posts since Apr 2007
Reputation Points: 30
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You