Huz*_*ifa 7 java truncate jdbc resultset
我试图使用JDBC从java中删除表中的数据.首先,我计算行数,确保表不为空,然后截断数据.
这是我正在使用的代码
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");
}
Run Code Online (Sandbox Code Playgroud)
错误:
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)
Run Code Online (Sandbox Code Playgroud)
错误发生在截断表dbo.Link上.
我这样做是对的吗?
请有人帮我这个.
谢谢.
rge*_*man 21
不要executeQuery
用来执行DDL语句; 用executeUpdate
.
引用链接的Javadocs:
执行给定的SQL语句,该语句可以是INSERT,UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,例如 SQL DDL语句.
(强调我的)
truncate table语句是一个DDL语句.