Set construction project JRE input path to 'Javase-1.7'

Asked

Viewed 62 times

0

I am trying to make a connection to Mysql database, but when I use PreparedStatement, eclipse returns: Definir projeto de construção JRE entrada caminho para 'JavaSE-1.7 Yes, I changed the compiler version to 1.7, but only works with Android 1.5 and 1.6 right? How can I connect my database? This is my real method to connect:

private ArrayList<String> sql = new ArrayList<String>();
private Connection con;
  public String getUsuario(String...parametros) throws SQLException {
            sql.add("SELECT "+parametros[1]+"FROM users WHERE `usuario_id` = "+parametros[0]+";");
               for (String string : sql) {
                try (PreparedStatement stm = con.prepareStatement(string);
                        ResultSet rs = stm.executeQuery()) {
                        while(rs.next()) {
                            System.out.println(rs.getString("usuario_senha"));
                        }
                    }
            }
            return "";
        }

1 answer

2

Your problem isn’t the PreparedStatementbecause it exists in Java 1.6.

The problem is in the block try.
The form you are using only exists in Java 1.7.

You need to use the traditional block shape try/catch

try{
    PreparedStatement stm = con.prepareStatement(string);
} catch(SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  • 1

    Two important comments. Ramaral is right and really its problem is the Try-with-Resources, however, since you are giving up on it don’t forget to close the Statement and the ResultSet in a block finally (what the code example in question is not doing).

  • You are absolutely right! In full implementation you have to ensure that when there is an error, objects are closed. The posted code was just to illustrate the block try/catch

Browser other questions tagged

You are not signed in. Login or sign up in order to post.