2014-10-27 81 views
1

正常工作我有這樣一個片段:closeOnCompletion不會在MySQL JDBC 5.1.32

Statement statement = connection.createStatement(); 
ResultSet rs = statement.executeQuery("SELECT * FROM table_A"); 

statement.closeOnCompletion(); 
System.out.println(statement.isCloseOnCompletion()); 

rs.close(); 
System.out.println(statement.isClosed()); 

closeOnCompletion被調用後,我想到的聲明被關閉,因爲它的結果集被關閉。但是,在檢查statement.isClosed()時它只顯示「false」。

我誤解了jdk文檔嗎?正如它所說的:closeOnCompletion()指定當它的所有依賴結果集關閉時,該Statement將被關閉。

=========================== 更新: 原來是我的疏忽。最後一條語句實際上返回了預期的「真」結果。因此,它不再是一個問題

+0

你不必在*'.executeQuery()'之前調用'.closeOnCompletion()'*嗎? – eggyal 2014-10-27 09:08:49

+0

對不起,我在檢查真實代碼中的結果時犯了一個錯誤。它實際上顯示「真正」時checkin statement.isClosed() – smonica 2014-10-29 07:08:59

回答

2

由於聲明是依賴於結果,我建議這個

Statement statement = connection.createStatement(); 
ResultSet rs = statement.executeQuery("SELECT * FROM table_A"); 

rs.close(); 
statement.close(); 
System.out.println(statement.isClosed()); 

這是適用的,如果你最終要的是關閉的語句,如果你的問題是,爲什麼說法不打烊,即使稱爲onexit,然後更好地引用javadoc。