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 "";
}
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 theResultSet
in a blockfinally
(what the code example in question is not doing).– Anthony Accioly
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
– ramaral