我正在尝试使用 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");
}
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)
错误出现在截断表 dbo.Link 处。
我这样做的方式正确吗?
有人可以帮我解决这个问题吗?
Thanks.
不要使用executeQuery
执行 DDL 语句;使用executeUpdate http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate%28java.lang.String%29.
引用链接的 Javadocs:
执行给定的 SQL 语句,该语句可以是 INSERT、UPDATE 或
DELETE 语句或不返回任何内容的 SQL 语句,例如SQL DDL 语句.
(强调我的)
截断表语句是 DDL 语句。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)