Boa tarde, está dando erro no meu try, na linha 3, a seguinte mensagem no Connection - "The resource type Connection does not implement java.lang.AutoCloseable". Obs.: Uso o java 11 e o Eclipse 2019-03.
package br.com.caelum.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestaInsercao {
public static void main(String[] args) throws SQLException {
try (Connection connection = Database.getConnection()) {
String sql = "insert into Produto (nome, descricao) values(?, ?)";
try(PreparedStatement statement = connection.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS)) {
adiciona("TV LCD", "32 polegadas", statement);
adiciona("Blueray", "Full HDMI", statement);
}
}
}
private static void adiciona(String nome, String descricao,
PreparedStatement statement) throws SQLException {
if (nome.equals("Blueray")) {
throw new IllegalArgumentException("Problema ocorrido");
}
statement.setString(1, nome);
statement.setString(2, descricao);
boolean resultado = statement.execute();
System.out.println(resultado);
ResultSet resultSet = statement.getGeneratedKeys();
while (resultSet.next()) {
String id = resultSet.getString("id");
System.out.println(id + " gerado");
}
resultSet.close();
}
}