JDBC優化技巧之一

2021-03-31 16:47:39 字數 1230 閱讀 7283

以下是一些常用的jdbc小技巧,也許可以提高你的系統的執行速度。

1. 當使用preparedstatement/callablestatement時,盡量使用它提供的setparams的方法。

下面是錯誤的方法:

callablestatement cstmt = conn.preparecall (

"");

resultset rs = cstmt.executequery ();

下面是正確的方法:

callablestatement cstmt - conn.preparecall (

"");

cstmt.setlong (1,12345);

resultset rs = cstmt.executequery();

2.使用批量更新的方法

一般都是使用這樣的方法更新:

preparedstatement ps = conn.preparestatement(

"insert into employees values (?, ?, ?)");

for (n = 0; n < 100; n++)

如果使用jdbc的高階功能的話,批量更新應該會有更高的效率:

preparedstatement ps = conn.preparestatement(

"insert into employees values (?, ?, ?)");

for (n = 0; n < 100; n++)

ps.executebatch();

3.正確使用resultset的get方法

盡量不要使用諸如rs.getobject()這樣的方法,另外如果還要更高的效率的話,請使用rs.getstring(1)這樣通過字段索引號的方式來取得字段值,而不是使用象rs.getstring("username")這樣的方法來取得字段值。

4.取得剛增加的記錄的自動增加型別的字段的值

如果你的jdbc驅動支援的話,有乙個很方便的方法可以取得自增字段的值,如下:

int rowcount = stmt.executeupdate (

"insert into localgeniuslist (name) values ('karen')",

statement.return_generated_keys);

resultset rs = stmt.getgeneratedkeys ();

關於JDBC的優化之一

以下是一些常用的jdbc小技巧,也許可以提高你的系統的執行速度。1.當使用preparedstatement callablestatement時,盡量使用它提供的setparams的方法。下面是錯誤的方法 callablestatement cstmt conn.preparecall resul...

linux技巧之一

實現redhat非正常關機的自動磁碟修復 先登入到伺服器,然後在 etc sysconfig裡增加乙個檔案autofsck,內容如下 autofsck def check yes prompt yes 改變檔案或目錄之最後修改時間 變為當前時間 執行格式 touch name name 可為檔案或目...

mysql 技巧之一

primary key 表的主鍵,同時會建立索引。mysql中每張table最多有乙個primary key,也可以沒有。primary key可以包含乙個column,也可以包含多個column。當包含多個column時,這些column的組合必須在table中唯一。unique key 唯一鍵,...