java.sql.SQLException: 在 mysql 结果集结束后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16418258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: After end of result set in mysql
提问by KhAn SaAb
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.
我正在尝试从 MYSQL 下载图像 (.png) 文件。有一段时间它工作正常。无法找到确切的问题。它在 Jboss 服务器上正常工作。尝试在我的本地机器上的 Apche 上运行时抛出错误。
Please help me to FIX the error. Here is my java code.
请帮我修复错误。这是我的java代码。
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)
采纳答案by Aashray
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.next()
两次电话。我假设您的查询仅返回 1 行,因为您正在尝试匹配 by Userid
。当第二个result.next()
被调用时,ResultSet
. 这就是SQLException
抛出an 的原因。result.next()
像这样删除第一个:
result = st.executeQuery();
if(!result.next()){
input = result.getAsciiStream(1);
}
回答by DaveH
The second result.next
is moving you past the end of the result set.
第二个result.next
是让你越过结果集的末尾。
I think you want
我想你想要
result = st.executeQuery();
if(result.next()){
input = result.getAsciiStream(1);
}