2011-07-26 24 views
2

我想通過使用prepareCall的JDBC連接來設置應用程序角色。這似乎好的工作(即語法明智的),但SQL Server 2008中返回此錯誤:JDBC set_approle

Application roles can only be activated at the ad hoc level 

我不是從一個存儲過程或任何距離我的JDBC連接射擊這一點,只需直接,因爲這樣的:

CallableStatement cstmt = con.prepareCall("{call sys.sp_setapprole(?, ?, ?, ?)}"); 
//setup all the IN and OUT parameters here 
cstmt.execute(); 

任何想法爲什麼這不起作用?

回答

1

事實證明,我使用的是JDBC驅動程序,微軟的JDBC驅動程序無法關閉Prepared Statements或Statement Pooling。因此,發送到數據庫的所有內容都被封裝在sys_sp_setapprole()檢測到並且不喜歡的sp_prepexec()中,因爲它不能被包裝在另一個過程中,並且必須直接在數據庫上執行。不幸的是,解決方案是使用另一個JDBC驅動程序。

+0

你使用了哪一個? –

+0

@SimonArsenault我最終與https://www.inetsoftware.de/products/jdbc-driver/ms-sql/merlia – Ayyoudy

2

使用下面的代碼來執行sp_setapprole,它對我來說很重要!

// substitute userName and password with your own code 
try { 
    String sql = "EXEC sp_setapprole '" + userName + "', '" + password + "'"; 
    Statement st = con.createStatement(); 
    st.execute(sql); 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} 
+0

一起如何以這種方式檢索OUTPUT參數? –