JDBC PreparedStatementの例 - レコードのリストを選択する

JDBC PreparedStatementを介してテーブルからレコードを選択し、ResultSetオブジェクトを介してレコードを表示する方法を示す例を次に示します。 selectクエリを発行するには、次のように `PreparedStatement.executeQuery()`メソッドを呼び出します。

String selectSQL = "SELECT USER__ID, USERNAME FROM DBUSER WHERE USER__ID = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);
ResultSet rs = preparedStatement.executeQuery(selectSQL );
while (rs.next()) {
    String userid = rs.getString("USER__ID");
    String username = rs.getString("USERNAME");
}

完全な例…​

package com.mkyong.jdbc;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCPreparedStatementSelectExample {

    private static final String DB__DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DB__CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
    private static final String DB__USER = "user";
    private static final String DB__PASSWORD = "password";

    public static void main(String[]argv) {

        try {

            selectRecordsFromTable();

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

    }

    private static void selectRecordsFromTable() throws SQLException {

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;

        String selectSQL = "SELECT USER__ID, USERNAME FROM DBUSER WHERE USER__ID = ?";

        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(selectSQL);
            preparedStatement.setInt(1, 1001);

           //execute select SQL stetement
            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {

                String userid = rs.getString("USER__ID");
                String username = rs.getString("USERNAME");

                System.out.println("userid : " + userid);
                System.out.println("username : " + username);

            }

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }

    private static Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(DB__DRIVER);

        } catch (ClassNotFoundException e) {

            System.out.println(e.getMessage());

        }

        try {

            dbConnection = DriverManager.getConnection(
                             DB__CONNECTION, DB__USER,DB__PASSWORD);
            return dbConnection;

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

        return dbConnection;

    }

}

結果

「user__id = 1001」のレコードのリストがテーブル「DBUSER」から検索され、表示されます。