该语句没有返回结果集。Java错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16348712/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:25:05  来源:igfitidea点击:

The statement did not return a result set. Java Error

javajdbcresultsettruncate

提问by Huzaifa

I am trying to delete data from a table from java using JDBC. First I am counting the no of rows and making sure the table is not empty and then Truncating the data.

我正在尝试使用 JDBC 从 java 中的表中删除数据。首先,我计算行数并确保表不为空,然后截断数据。

Here is the code I am using

这是我正在使用的代码

  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection con = DriverManager.getConnection("jdbc:sqlserver://m-i:1433;databaseName=Tes", "sa", "Password");
    Statement cnnt= con.createStatement();
    Statement del1 = con.createStatement();
    ResultSet rs = cnnt.executeQuery("Select count(lea) AS cnt from dbo.Link");
   int count= 0;
    if(rs.next())
    {
        count = rs.getInt("cnt");
    }
  System.out.println(count);
 if(count != 0)
 {
   del1.executeQuery("Truncate Table dbo.Link");
 }
else
   {
       System.out.println("Table is already empty");
   }

Error:

错误:

 Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:800)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:616)

The error is at the Truncate Table dbo.Link.

错误位于截断表 dbo.Link。

Am I doing this the right way?

我这样做是否正确?

Can someone help me with this please.

有人可以帮我解决这个问题吗?

Thanks.

谢谢。

采纳答案by rgettman

Don't use executeQueryto execute a DDL statement; use executeUpdate.

不要executeQuery用于执行 DDL 语句;使用executeUpdate.

To quote from the linked Javadocs:

引用链接的 Javadocs:

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

执行给定的 SQL 语句,它可以是 INSERT、UPDATE 或 DELETE 语句,也可以是不返回任何内容的 SQL 语句,例如 SQL DDL 语句

(emphasis mine)

(强调我的)

And a truncate table statement is a DDL statement.

截断表语句是 DDL 语句。