khAn khAn - 1 year ago 111
Java Question

java.sql.SQLException: After end of result set in mysql

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();;

input = result.getAsciiStream(1);
FileOutputStream output = new FileOutputStream(getSignatureImageDestinationPath());
int ch =;
while (ch != -1) {
output.write((char) ch);
ch =;
} catch (Exception e) {
} finally {
if (st != null) {

Here is Stack trace output:

java.sql.SQLException: After end of result set
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.ResultSetImpl.checkRowPos(
at com.mysql.jdbc.ResultSetImpl.getAsciiStream(
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getAsciiStream(
at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getAsciiStream(
at com.ninenexus.model.Signature.getSignatureImage(
at com.ninenexus.servlets.SaveMySignature.mySignatureDisplay(
at com.ninenexus.servlets.SaveMySignature.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$ Source)
at Source)

Answer Source

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

result = st.executeQuery();
    input = result.getAsciiStream(1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download