• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with servlet reading images in database!

 
karthik suresh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people,
There is a problem with servlet reading images in database.
Database used :Ms-Access
Exception is:[ Microsoft ][ODBC Driver Manager]Invalid cursor state
Code:
************************************************
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Dbreader extends HttpServlet
{
Connection con;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:jspdata");
}
catch(Exception e)
{System.out.println("Exception is!"+e);
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try{
res.setContentType("image/gif");
ServletOutputStream out=res.getOutputStream();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from ImageTable where imageid=1");
BufferedInputStream gifData=new BufferedInputStream(rs.getBinaryStream("Image"));
System.out.println("value of gif"+gifData.toString());
byte buf[]=new byte[4*1024];
int len;
while((len=gifData.read(buf,0,buf.length))!=-1)
{out.write(buf,0,len);
}
}
catch(Exception e)
{System.out.println("Exception !"+e);}
}
}
Please help me!
Thanx in advance...
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik
When you get a ResultSet from your execute method the cursor is postioned before the first record in the ResultSet, you have to move it to the first record in order to use it. Also, if there is no records in the result set you'll get an error too.
Try this:

You might want to change the code below too so that it prints out something meaningfull in case there is no image found.
hope that helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!