7

I am trying to Download image (.png) file from MYSQL. some time it works fine.unable find exact problem. it works properly on Jboss server. throws an error while trying to run in my local machine on Apche.

Please help me to FIX the error. Here is my java code.

    try {
        conection = SQLUtil.createConnection(Constant.DataSourceName);
        st = conection.prepareStatement("SELECT image FROM TABLE_NAME WHERE Userid="+ getUserId());
        result = st.executeQuery();
        result.next();

        if(!result.next()){
        input = result.getAsciiStream(1);
        }
        FileOutputStream output = new FileOutputStream(getSignatureImageDestinationPath());
        int ch = input.read();
        while (ch != -1) {
            output.write((char) ch);
            ch = input.read();
        }
        output.close();
        input.close();
        result.close();
        SQLUtil.closeConnection(conection);
    } catch (Exception e) {
        System.out.println(e+":Error");
    } finally {
        if (st != null) {
            SQLUtil.closeConnection(conection);
        }
    }

Here is Stack trace output:

java.sql.SQLException: After end of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)
at com.mysql.jdbc.ResultSetImpl.getAsciiStream(ResultSetImpl.java:1275)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getAsciiStream(DelegatingResultSet.java:253)
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getAsciiStream(DelegatingResultSet.java:253)
at com.ninenexus.model.Signature.getSignatureImage(Signature.java:173)
at com.ninenexus.servlets.SaveMySignature.mySignatureDisplay(SaveMySignature.java:69)
at com.ninenexus.servlets.SaveMySignature.doPost(SaveMySignature.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
5
  • 1
    where is your stacktrace? Commented May 7, 2013 at 11:50
  • 1
    What's the error, specifically, when it doesn't work? Be sure to close all resources in the finally block. Also, why are you using characters to represent binary data? Commented May 7, 2013 at 11:50
  • Marco Forberg.in cath block Commented May 7, 2013 at 11:51
  • BDKosher .i am closing all the opened connections.. Commented May 7, 2013 at 11:53
  • Possibly not related to your problem, but your use of result.next is unusual. If your query returns more than one row then "input" will be left at whatever its initial value is. Commented May 7, 2013 at 11:54

2 Answers 2

15

You are calling result.next() twice. I'm assuming that your query returns only 1 row since you are trying to match by Userid. When the second result.next() is being called, there is no row to be returned in the ResultSet. This is why an SQLException is being thrown. Remove the 1st result.next() like so:

result = st.executeQuery();
if(!result.next()){
    input = result.getAsciiStream(1);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

That still won't work will it? It's saying if NOT result.next().
Doesn't matter, the call itself will trigger the exception.
OK, I can see that the exception will be throw by the second call. But lets say he removes the first call to result.next(). The remaining call will return true, meaning that the input variable will not be populated won't it?
Logically, I don't know what he's trying to do, but it won't enter the if block if the result set is not empty.
2

The second result.next is moving you past the end of the result set.

I think you want

result = st.executeQuery();
if(result.next()){
   input = result.getAsciiStream(1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.