How can i do retrieval of image from database(mysql with blob data) and set it in table form(PrintWriter pw = response.getWriter(); pw.write("<img src=""/>"); )
at servlet and display it at JSP page, making a table dynamically(base on my database data)(Although i'm not sure whether can it be achievable) But if it is achievable, how can i do that? If it is not achievable. What is the suggested possible answer? Your help will be much appreciated.
Here is my code :
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] binaryStream;
ArrayList<RetrievingPost> combine=new ArrayList<RetrievingPost>();
try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/asq?characterEncoding=UTF-8&useSSL=false", "root", "root");
// Execute SQL query
Statement pst = connection.createStatement();
String sql;
sql = "select profilepic, displayname, r.userId, friendId from registration r inner join friendlist fl on r.userId=fl.userId where r.emailAddress='" + emailAddress + "'";
ResultSet rs = pst.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
Retrieving retrieve=new Retrieve();
userId = rs.getInt("userId");
System.out.println(userId+"---------userId-------");
friendId =rs.getInt("friendId");
displayname=rs.getString("displayname");
binaryStream = rs.getBytes("profilepic");
retrieve.setFriendId(friendId);
retrieve.setDisplayName(displayname);
retrieve.setBinaryStream(binaryStream);
Combine.add(retrieve);
}
// Clean-up environment
rs.close();
pst.close();
connection.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
} //end try
for(int j=0;j<combine.size();j++){
PrintWriter pw = response.getWriter();
pw.write("<HTML><BODY>");
pw.write("<TABLE border='1'>");
pw.write("<TR>");
pw.write("<TD>");
pw.write(combine.get(i).getDisplayname());
pw.write("</TD>");
//if possible, i would like to display my image into the table
pw.write("<TD>");
String tPosted=combine.get(i).getTimePosted().toString();
pw.write(tPosted);
pw.write("</TD>");
pw.write("</TR>");
}}
getServletContext().getRequestDispatcher("/DisplayingImage.jsp").include(request, response);}
This is my bean
package registration;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
import javax.servlet.http.Part;
public class RetrievingPost {
int userId;
int friendId;
String privacyDesc;
String status;
String description;
Timestamp timePosted;
String category;
String displayname;
byte[] binaryStream ;
public byte[] getBinaryStream() {
return binaryStream;
}
public void setBinaryStream(byte[] binaryStream) {
this.binaryStream = binaryStream;
}
public String getDisplayname() {
return displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public RetrievingPost(int userId,Timestamp timePosted, String privacyDesc, String status, String post, int friendId){
this.userId=userId;
this.timePosted=timePosted;
this.privacyDesc=privacyDesc;
this.status=status;
this.description=description;
this.friendId=friendId;
}
public RetrievingPost(){
super();
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getFriendId() {
return friendId;
}
public void setFriendId(int friendId) {
this.friendId = friendId;
}
public String getPrivacyDesc() {
return privacyDesc;
}
public void setPrivacyDesc(String privacyDesc) {
this.privacyDesc = privacyDesc;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Timestamp getTimePosted() {
return timePosted;
}
public void setTimePosted(Timestamp timePosted) {
this.timePosted = timePosted;
}
}