簡體   English   中英

如何從prepareCall方法中獲取自定義CallableStatement對象

[英]How can I get a custom CallableStatement object out of a prepareCall method

我想創建一個擴展CallableStatement對象的子類。 我想這樣做,所以我可以覆蓋execute和executeQuery方法來跟蹤每個SP調用的一些指標。

目前我的代碼如下所示:

Connection db = poolingDataSource.getConnection();
CallableStatement cstmt = db.prepareCall("{call pSampleStoredProc()}");
ResultSet rs = cstmt.executeQuery();

其中poolingDataSource來自apache commons dbcp包。 我的實現使用JDBC連接到MySQL數據庫。

目前,prepareCall方法返回com.mysql.jdbc.JDBC4CallableStatement。 我希望能夠更改它,以便它返回我自己的類,擴展JDBC4CallableStatement但覆蓋execute()和executeQuery()方法。

關於最佳方法的任何想法?

如果你有一個可管理的地方你需要這樣做,我建議一個簡單的方法(比試圖讓數據庫驅動程序返回你的自定義CallableStatement對象更容易)將使包裝或裝飾器包裹您從db獲得的CallableStatement對象。 基本上創建一個實現CallableStatement的所有方法的類,將CallableStatement對象作為其構造函數的參數,並將所有方法調用直接委托給另一個對象,但是對於execute()和executeQuery(),你也是在委托給其他對象execute()方法之前和之后調用您的日志記錄方法。

當然還有其他方法可以做到這一點,這讓我感到更輕松。

Connection db = poolingDataSource.getConnection();
CallableStatement cstmt = db.prepareCall("{call pSampleStoredProc()}");
MyCallableStatement mystmt = new MyCallableStatement( cstmt );
ResultSet rs = mystmt.executeQuery();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM