7

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.

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.

Am I doing this the right way?

Can someone help me with this please.

Thanks.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Huzaifa
  • 1,111
  • 5
  • 20
  • 37

1 Answers1

23

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

To quote from the linked 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.

(emphasis mine)

And a truncate table statement is a DDL statement.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • @Smit - The select isn't the problem. The OP states "The error is at the Truncate Table dbo.Link.". – rgettman May 02 '13 at 22:31
  • Wow. Thanks a ton. I did not see that. – Huzaifa May 02 '13 at 22:34
  • @rgettman Previous comment was valid before you updated the answer. I will delete that comment. – Smit May 02 '13 at 22:37
  • @rgettman Thanks for posting a very useful answer for a beginner like me. I have a simplequestion releted to my java code. Here is the question which i asked on stakoverflow- what ( com.microsoft.sqlserver.jdbc.SQLServerException:The index 0 is out of range )exception means. I just wanted to know that is there any way by which i can get column number automatically from the db instead of putting column numbers manually in my first for loop – Shantanu Nandan May 04 '14 at 17:05