该语句未返回结果集. Java错误 [英] The statement did not return a result set. Java Error

查看:146
本文介绍了该语句未返回结果集. Java错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我正在使用的代码

  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");
   }

错误:

 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)

错误在截断表" dbo.Link中.

The error is at the Truncate Table dbo.Link.

我这样做正确吗?

有人可以帮我吗?

谢谢.

推荐答案

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

Don't use executeQuery to execute a DDL statement; use executeUpdate.

要引用链接的Javadocs:

To quote from the linked Javadocs:

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

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.

(重点是我的)

截断表语句是DDL语句.

And a truncate table statement is a DDL statement.

这篇关于该语句未返回结果集. Java错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆