I try to kill a MS sql server (8) process with a prepared statement in java. But I always receive the exception: com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: Incorrect syntax near '@P0'.
Any ideas?
public void killBlockingProcess(int spid) {
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("kill ?");
ps.setInt(1, spid);
boolean res=ps.execute();
}
catch (Exception ex) {
logger.error(this + ",killBlockingProcess: " + ex.getMessage());
}
finally
{
try {ps.close(); } catch (Exce开发者_高级运维ption exp){}
}
}
kill
accepts only numbers, not parameters. Run:
"kill " + spid
Against the connection and it should work. Alternatively, let SQL expand the parameter:
"declare @query varchar(max)
set @query = 'kill ' + ?
exec (@query)"
精彩评论