MySQL - java.sql.SQLException: ResultSet 来自 UPDATE。没有数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32682916/
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
MySQL - java.sql.SQLException: ResultSet is from UPDATE. No Data
提问by RandomWanderer
public static void main(String args[])
{
SQLConnector sqlConnect = new SQLConnector();
Connection conn = null;
try
{
conn= sqlConnect.getConnection();
CallableStatement cStmt = conn.prepareCall("{ call test(?,?,?)}");
cStmt.setDouble(1, 100.0);
cStmt.setInt(2, 1);
cStmt.registerOutParameter(3, java.sql.Types.VARCHAR);
ResultSet rs = cStmt.executeQuery();
if (rs.next()) {
System.out.println(rs.getString(3);
}
cStmt.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
sqlConnect.closeConnection(conn);
}
}
This snippet throws the error
此代码段引发错误
java.sql.SQLException: ResultSet is from UPDATE. No Data.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
But if I pass the same parameters from MySQL Workbench it gives proper output.
但是如果我从 MySQL Workbench 传递相同的参数,它会给出正确的输出。
I am using MySQLServer 5.6.25 and MySQLConnector 5.1.6.
我正在使用 MySQLServer 5.6.25 和 MySQLConnector 5.1.6。
Please help me solve this problem. This looks like a bug in the way I call MySQL from Java
请帮我解决这个问题。这看起来像是我从 Java 调用 MySQL 的方式中的一个错误
回答by Andreas
See section 4 of this MySQL documentation page: Using JDBC CallableStatements to Execute Stored Procedures.
请参阅此 MySQL 文档页面的第 4 节:使用 JDBC CallableStatements 执行存储过程。
You don't use executeQuery()
. You have to use execute()
and getResultSet()
.
你不使用executeQuery()
. 您必须使用execute()
和getResultSet()
。
Since you knowthe statement will return exactly one ResultSet
, your code becomes:
由于您知道该语句将只返回 one ResultSet
,因此您的代码变为:
cStmt.execute();
try (ResultSet rs = cStmt.getResultSet()) {
if (rs.next())
System.out.println(rs.getString(3));
}
Except of course you might be wrong, because the call doesn't return a result set, but instead returns a string in an output parameter, and that totally changes the code:
当然你可能错了,因为调用不返回结果集,而是在输出参数中返回一个字符串,这完全改变了代码:
cStmt.execute();
System.out.println(cStmt.getString(3));
回答by developer
Remove the below line in your code to resolve the issue:
删除代码中的以下行以解决问题:
cStmt.execute();
cStmt.execute();
回答by Sachin Pardeshi
it's all about changes in your procedure
一切都与您的程序发生变化有关
remove into declared_veritablefrom procedure
从过程中删除到clarified_veritable
because at the end you will get all data from result-set only so no need to provide into clause in your select query of procedure.
因为最后您将仅从结果集中获取所有数据,因此无需在您的程序选择查询中提供 into 子句。