<% response.s" name="description">

How to display multiple images using blob from mysql database. The program code is as below:
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;"> <% response.setContentType("image/gif"); OutputStream o = response.getOutputStream(); while (res.next()) { %> <TR>
<TD><% image = res.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
o.write(imgData);
%> <TD><%=res.getInt(2)%>
</TR> <% } %> <% // close all the connections. o.flush(); o.close(); res.close(); statement.close(); connection.close();
} catch (Exception ex) {

1)images:
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<HTML>
<table border="1">
<tr><th>ID</th><th>Image</th></tr>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt=con.createStatement();
String strQuery = "select * from data";
ResultSet rs = stmt.executeQuery(strQuery);
while(rs.next()){
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td>
<img src="image.jsp?imgid=<%=rs.getInt(1)%>" width="100" height="100">
</a></td>
</tr>
<%
}
rs.close();
con.close();
stmt.close();
}
catch(Exception e)
{
e.getMessage();
}
%>
</table>
</HTML>
2)image.jsp:
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<%
int id = Integer.parseInt(request.getParameter("imgid"));
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st=con.createStatement();
String strQuery = "select image from data where id="+id;
ResultSet rs = st.executeQuery(strQuery);
String imgLen="";
if(rs.next()){
imgLen = rs.getString(1);
}
rs = st.executeQuery(strQuery);
if(rs.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
st.close();
response.reset();
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.