4
respostas

Dando erro no código.

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();
    }

}
4 respostas

Boa tarde, Thiago! Como vai?

Vc poderia colar aqui o código completo da classe TestaInsercao incluindo a parte onde as importações são feitas juntamente com o código da classe Connection? Assim eu poderei entender melhor o que está acontecendo pra tentar te ajudar!

Pronto, se quiser coloco a classe Database também.

Thiago, o seu código não apareceu! Vc poderia tentar enviar novamente? Sempre que for postar um código, utilize o botão "inserir código" e cole o código no lugar indicado.

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();
    }

}

Pronto.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software