How to retreive images from mysql database into the portlet
i have used following code
package com.test;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class AccessDBI
{
// create SQL strings to query database tables
// get subject names from database
public void getImage() throws Exception
{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
Connection con = null;
try {
String url = "jdbc:mysql://localhost/test";
con = DriverManager.getConnection(url,"root","");
// prepare the SQL query to get subject name and id
PreparedStatement pst=con.prepareStatement("select image from images");
ResultSet rs = pst.executeQuery();
while(rs.next())
{
FileOutputStream fo = new FileOutputStream("/images/theimage.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fo);
bos.write(rs.getBytes("image"));
bos.close();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
// close the connection so it can be returned to
// the connection pool then return the list
con.close();
}
}
}
and in the view.jsp
i have img src="/images/theimage.jpg"
yet i am unable to display the images
(question 2)
if i need to diplay the image as in the conventional way of
jsp(WEB INF) where can i store the image so that to display in the protlet