ResultSetオブジェクトからの結果の取得¶
A ResultSet object is the primary storage mechanism for the data
returned by an SQL statement. Each ResultSet object contains both data
and metadata (in the form of a ResultSetMetaData object).
ResultSetMetaData includes useful information about results returned by
the SQL command: column names, column count, row count, column length,
and so on.
To access the row data stored in a ResultSet object, an application
calls one or more getter methods. A getter method retrieves the
value in particular column of the current row. There are many different
getter methods; each method returns a value of a particular type. For
example, the getString() method returns a STRING type; the getDate()
method returns a Date, and the getInt() method returns an INT type. When
an application calls a getter method, JDBC tries to convert the value
into the requested type.
各 ResultSet は、現在の行を指す内部ポインターを保持します。 executeQuery() メソッドが ResultSet を返すと、ポインターは最初の行の前に配置されます。アプリケーションがポインターを移動する前に getter メソッドを呼び出すと、 getter メソッドは失敗します。次の(または最初の)行に進むには、 ResultSet’s next() メソッドを呼び出します。 ResultSet.next() はブール型メソッドです。 ResultSet に別の行がある場合は TRUE を返し、最後の行を超えて移動した場合は FALSE を返します。
ポインターを最初の行に移動した後、サンプルアプリケーションは getString() getter メソッドを使用して最初の列の値を取得し、その値を出力します。 ListEmployees はループで rs.next() と rs.getString() を呼び出すため、結果セットの各行を処理します。 ListEmployees は、 rs.next() がポインターを最後の行を超えて移動し、 FALSE を返すとループを終了します。
while(rs.next())
{
System.out.println(rs.getString(1));
}
ResultSet インターフェースを使用するときは、次のことに注意してください:
値を読み込む前に
next()を呼び出す必要があります。別の行が利用可能な場合、next()はtrueを返し、処理のために行を準備します。JDBC仕様では、アプリケーションは
ResultSetの各行に1回だけアクセスする必要があります。現時点では、AdvancedServerJDBCドライバーを使用すると、必要な回数だけフィールドにアクセスできますが、この規則を守るのが最も安全です。ResultSetの使用が終了したら、close()メソッドを呼び出して、そのオブジェクトが保持しているリソースを解放します。