Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Problem retrieving image from database

    hello.. i'm bhavani,
    I'm facing problem in retrieving image from the database. I'm using oracle 10g,netbeans 7.0.1,jdk-1.7,jre-1.4. I'm able to store the image into db successfully bt coun't retrieve it. It is showing the following error..

    java.sql.SQLException: General error

    database structure;
    Java Code:
     create table pro(image blob,inum integer);
    storing image:

    upload.html:

    Java Code:
    <!DOCTYPE html>
    
    <html>
    
        <head>
    
            <title></title>
    
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        </head>
    
        <body>
    
            	<form action="store.jsp" method="post">
    	image id : <input type="text" name="t1"><br>
    	image path:<input type="text" name="f1"><br>
    	<br>
    	<input type="submit" value="upload">
    	</form>
        </body>
    
    </html>
    store.jsp:

    Java Code:
    <!DOCTYPE html>
    
    <html>
    
        <head>
    
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
            <title>JSP Page</title>
    
        </head>
    
        <body>
    
            <%!
     
           	String msg="";
           	Connection conn=null;
      
       
           	ResultSet rs=null;
     
           	PrintWriter pw=null;
    
    	FileInputStream fis=null; 
           %>
    
          <%
    	String imagefilepath=request.getParameter("f1");
    	String imageid=request.getParameter("t1");
    	System.out.println(imagefilepath);
    	try{
    	 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
           	 conn=DriverManager.getConnection("jdbc:odbc:bhavani","system","password");
    
           	String sql="insert into pro values(?,?)";
    	PreparedStatement stmt=conn.prepareStatement(sql);
    	File img=new File(imagefilepath);
    	fis=new FileInputStream(img);
    	stmt.setBinaryStream(1,fis,(int)img.length());
    	stmt.setString(2,imageid);
    	stmt.execute();
    	out.println("success");
    	fis.close();
    	conn.close();
    	}
    	catch(Exception e)
    	{
    	out.println(e);
    	}
            %>
    	
    
    
        </body>
    
    </html>
    ret.html:

    Java Code:
    <!DOCTYPE html>
    
    <html>
    
        <head>
    
            <title></title>
    
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        </head>
    
        <body>
    
            	<form action="retrieve.jsp" method="post">
    	image id : <input type="text" name="t1"><br>
    	<input type="submit" value="retrive">
    	</form>
        </body>
    
    </html>
    retrieve.jsp:

    <!DOCTYPE html>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>JSP Page</title>

    </head>

    <body>


    <%!

    String msg="";
    Connection conn=null;


    ResultSet rs=null;

    PrintWriter pw=null;
    FileOutputStream fos=null;


    %>

    <%
    try{
    String imageid=request.getParameter("t1");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn=DriverManager.getConnection("jdbc:odbc:bhavan i","system","password");
    String sql="select image from pro where inum="+imageid;
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);
    try
    {
    rs.next();
    pw.println("32w45");
    }
    catch(Exception e)
    {
    pw.println("jhgkjgh");
    }
    //String IMGID=rs.getInt(1);
    File image=new File("E:\\RetrievedImag.jpg");
    fos=new FileOutputStream(image);
    byte[] buffer=new byte[1];
    InputStream is=rs.getBinaryStream(1);
    while(is.read(buffer)>0)
    fos.write(buffer);
    fos.close();
    conn.close();
    %>
    <img src="E:\\RetrieveImag.jpg" width=400 height=400>
    <%
    }
    catch(Exception e)
    {
    pw.println(e);
    }
    %>
    </body>

    </html>[/code]


    please help me to successfully retrieve the image..
    thanks in advance...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem retrieving image from database

    Removed from Image retrieval . When you have a question, start your own thread. Don't hijack another poster's thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Problem retrieving image from database

    First, what is the full excpetion plus stack trace.
    If you have no stack trace, then I suggest first ensuring you at least have a printStackTrace() call in any catch blocks.

    Second, what exactly do you expect to happen with this code?
    Because I'm not convinced it's going to do what you think, even if you solve the exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    i'm sorry.. actually i'm new to this.. i dont know how to post that's why.. thanku for the information

  5. #5
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    when i'm trying to insert into the db.. it is displaying success bt while retrieving now showing nullpointerexception as follows ...

    WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
    java.lang.NullPointerException
    at org.apache.jsp.retrieve_jsp._jspService(retrieve_j sp.java:129)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:847)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:403)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:492)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:378)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:847)
    at org.apache.catalina.core.StandardWrapper.service(S tandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipel ine.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPip eline.invoke(PESessionLockingStandardPipeline.java :91)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doServ ice(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMappe r.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(P rocessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(Proce ssorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(Process orTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute (DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtoc olFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(Defau ltProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(Defau ltProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(Htt pProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(Pr otocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(Selec tionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:7 1)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doW ork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run (AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

  6. #6
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    thanks for your reply Tolls..
    whats my target is to retrieve the image from the database and display it on the browser.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Problem retrieving image from database

    In which case you do not want to do this with a JSP.
    It won't work.
    It needs to be a servlet that streams the data direct to the browser via the response.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    means i have to do it only by using servelets..? please explain me clearly dont mind...

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Problem retrieving image from database

    That JSP above will already have started sending stuff back to the browser.
    You cannot, therefore, use it to send an image as well.
    You want the image to be sent as a separate thing...which is how the <img> tag works.

    So you need a servlet, which will get the image from the database and stream it to the browser using the response's output stream.
    The advantage of this (amongst other things, like actually working) is that if there's an error you will get a useful stack trace that points to real code and not server generated JSP classes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    ok i will try thank you...

  11. #11
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem retrieving image from database

    hello..
    finally replacing type-1 driver with type-4 driver i got the image retrieval.. bt the problem is.. its displaying blank image.. now what should i do..?
    i didnt change my code jst edited the driver and url..

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Problem retrieving image from database

    Has the image written successfully?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 01-25-2012, 05:10 PM
  2. Problem in retrieving values from Oracle 10g database
    By techsing14 in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 04-13-2011, 01:50 AM
  3. retrieving/uploading image
    By stegano in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 03:56 AM
  4. Replies: 1
    Last Post: 04-05-2011, 08:28 AM
  5. Retrieving data from database every 10 secs.
    By vnsuresh.abi in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-31-2011, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •