i want to display multiple images on brower including string values from sql database using jsp.
Hi Friend,
Try the following code:
1)images.jsp:
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<HTML>
<table>
<%
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 id from image";
ResultSet rs = stmt.executeQuery(strQuery);
while(rs.next()){
%>
<tr>
<td align="center">
<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 images from image 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();
}
%>
Thanks
hi friend
my project aim is to display all the data including images at a time on the browser from the database(sql) at a time .so is there any change to get the all the images and sno,imagename , etc(table fields:sno,sname,image,description).plz send complete code revert to this mail. i am very thankful to u as early as possible fnd.
thanks ®ards M Jagan