-3

I get an error saying "Resource references are not supported at language level '1.8'" as well as this:

Error:(62, 31) java: <identifier> expected
Error:(62, 32) java: ')' expected
Error:(62, 33) java: '{' expected
Error:(62, 35) java: not a statement
Error:(62, 40) java: ';' expected
Error:(64, 9) java: illegal start of expression
Error:(71, 11) java: 'catch' without 'try'
Error:(82, 5) java: illegal start of expression
Error:(82, 13) java: illegal start of expression
Error:(82, 36) java: ';' expected
Error:(82, 66) java: ';' expected
Error:(98, 2) java: reached end of file while parsing

1 Answers1

1

You can't use statements within a try resource, only resource is allowed. And here stmt.setString(1, state); is a statement. So you either need to create multiple try-catch or a separate method like below:

try(Connection con = getConnection();
                PreparedStatement stmt = createPreparedStatement(con, state);

                ResultSet rs = stmt.executeQuery();
        )
        {
        while (rs.next()) {
            customers.add(new Customer(rs.getInt("CustomerID"),
                    rs.getString("Name"),
                    rs.getString("City"),
                    rs.getString("StateProvince")));
        }
    }catch (SQLException e) {
        e.printStackTrace();
    }

The method:

PreparedStatement createPreparedStatement(Connection con, String state) throws SQLException {
        PreparedStatement stmt = con.prepareStatement("SELECT CustomerID, Name, City, StateProvince FROM CUSTOMER WHERE StateProvince LIKE ?");
        stmt.setString(1, state);
        return stmt;
    }
Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
  • I get NullPointerException – KobiashiMaru Nov 26 '17 at 05:21
  • @KobiashiMaru That might be for some other reason. This code is not tested as I don't have all your code. The problem you had earlier should get resolved by this approach. `NullPointer` is runtime exception.Anyway where are you getting the exception? – Shubhendu Pramanik Nov 26 '17 at 05:23
  • It is happening at jdbc.FiredUpDB.readCustomerBasics(FiredUpDB.java:56) at jdbc.FiredUpDB. FiredUpDB.readCustomers(FiredUpDB.java:26) at jdbc.Main.main(Main.java:16) – KobiashiMaru Nov 26 '17 at 06:04
  • I will probably just start another thread if I can't figure it out, thanks for the help. – KobiashiMaru Nov 26 '17 at 06:04