开发者

Kill MS SQL process from java prepared statement?

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)"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜