Any time you want to issue SQL statements to the database, you require a Statement instance. Once you have a Statement, you can use the executeQuery() method to issue a query. This will return a ResultSet instance, which contains the entire result.
SQL 文をデータベースに発行する時には必ず、Statement インスタンス が必要です。Statement インスタンスを持っていれば、executeQuery() メソッドを使って問い合わせを発行できます。このメソッドは、結果全 体を持った ResultSet インスタンスを返します。
The following must be considered when using the Statement interface:
Statement インタフェースを使用する際に以下を考慮しなければなりま せん。
You can use a Statement instance as many times as you want. You could create one as soon as you open the connection, and use it for the connections lifetime. You have to remember that only one ResultSet can exist per Statement.
必要な時には何回でも 1 つの Statement インスタンスを使用できます。 接続を開いたらすぐにこのインスタンスを作成でき、この接続が有効な期 間内で使用することができます。1 つの Statement には 1 つの ResultSet しかあり得ないことを覚えておかなければなりません。
If you need to perform a query while processing a ResultSet, you can simply create and use another Statement.
ResultSet の処理中に問い合わせを実行する必要があれば、単純に別の Statement を作成して、それを使用して下さい。
If you are using Threads, and several are using the database, you must use a separate Statement for each thread. Refer to the sections covering Threads and Servlets later in this document if you are thinking of using them, as it covers some important points.
スレッドを使用し、複数スレッドがデータベースを使用する場合、各スレッ ドに Statement インスタンスを分けて使わなければなりません。このような 使用を考えているのでしたら、この文書の後で記述する、スレッドとサーブレ ットを参照して下さい。そこにはいくつか重要な点が記載されています。
The following must be considered when using the ResultSet interface:
ResultSet インタフェースを使用する際、以下のことを考慮しなければな りません。
Before reading any values, you must call next(). This returns true if there is a result, but more importantly, it prepares the row for processing.
値を読む前に必ず、next() を呼び出して下さい。 結果があれば、真を返します。しかし、これにより処理のための行の準備 が行なわれるということがもっと重要です。
Under the JDBC spec, you should access a field only once. It's safest to stick to this rule, although at the current time, the Postgres driver will allow you to access a field as many times as you want.
JDBC の仕様では、一度しかフィールドにアクセスで きません。この規則を厳守することは最も安全なのですが、現時点での Postgres のドライバでは、必要ならば何回 でもフィールドにアクセスすることができます。
You must close a ResultSet by calling close() once you have finished with it.
ResultSet に対する処理が完了した時には close() を呼び出して ResultSet を閉じなければ なりません。
Once you request another query with the Statement used to create a ResultSet, the currently open instance is closed.
ResultSet を作成する時に使用していた Statement に別の問い合わせを 要求すると、その時点で開いていたインスタンスは閉ざされます。
An example is as follows:
例を以下に示します。
Statement st = db.createStatement(); ResultSet rs = st.executeQuery("select * from mytable"); while(rs.next()) { System.out.print("Column 1 returned "); System.out.println(rs.getString(1)); } rs.close(); st.close();