Querying columns in a SQL command and not using it induce unnecessary RAM usage and network transfer.

Noncompliant Code Example

private static final String QUERY = "SELECT id, first, last, age FROM Registration"; // Noncompliant {{Avoid querying SQL columns that are not used}}

public void callJdbc() {

    try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(QUERY);) {
        while (rs.next()) {
            // Display values
            System.out.print("ID: " + rs.getInt("id"));
            System.out.print(", First: " + rs.getString("first"));
            System.out.println(", Last: " + rs.getString("last"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

Compliant Solution

private static final String QUERY = "SELECT id, first, last, age FROM Registration";

public void callJdbc() {

    try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(QUERY);) {
        while (rs.next()) {
            // Display values
            System.out.print("ID: " + rs.getInt("id"));
            System.out.print(", Age: " + rs.getInt("age"));
            System.out.print(", First: " + rs.getString("first"));
            System.out.println(", Last: " + rs.getString("last"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

}