Kill an MS SQL process from a prepared Java message?

I am trying to kill a MS sql server (8) process using a prepared statement in java. But I always get an exception: com.microsoft.sqlserver.jdbc.SQLServerException: line 1: incorrect syntax next to '@ 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 (Exception exp){}
    }
}
+3
source share
1 answer

killaccepts only numbers, not parameters. Run:

"kill " + spid

Against the connection, and it should work. Alternatively, let SQL extend the parameter:

"declare @query varchar(max)
set @query = 'kill ' + ?
exec (@query)"
+1
source

All Articles