[OCPJP]try-with-resources敘述的用法4

Given the structure of the STUDENT table:

Student (id INTEGER, name VARCHAR)

Given:

Assume that:

The required database driver is configured in the classpath.
The appropriate database is accessible with the URL, userName, and passWord exists.
The SQL query is valid.

What is the result?

A. The program executes successfully and the STUDENT table is updated with one record.
B. The program executes successfully and the STUDENT table is NOT updated with any record.
C. A SQLException is thrown as runtime.
D. A NullPointerException is thrown as runtime.

題解

Connection物件是try-with-resources結構的資源,因此在離開try-with-resources結構的try區塊時會自動呼叫其close方法。由於Connection已經close,所以在第14行,嘗試使用Connection物件來建立Statement物件時,會拋出SQLException。

關於作者

Magic Len

各位好,我是Magic Len,是這網站的管理員。我是台灣台中大肚山上人,畢業於台中高工資訊科和台灣科技大學資訊工程系,曾在桃機航警局服役。我熱愛自然也熱愛科學,喜歡和別人分享自己的知識與經驗。如果你有興趣認識我,可以加我的Facebook,並且請註明是從MagicLen來的。

相關文章