Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Generated keys not requested.

Bom estou com um ecxeption que não estou conseguindo resolver:

Cod:

import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

public class testaInsercao {

public static void main(String[] args) throws SQLException {

    ConnectionFactory factory = new ConnectionFactory();
    Connection connection = factory.recuperarConexao();

    Statement stm = connection.createStatement();
    stm.execute("INSERT INTO Produto (nome, descricao) VALUES('mouse', 'mouse sem fio')"
            , Statement.NO_GENERATED_KEYS);
    ResultSet rst = stm.getGeneratedKeys();
    while (rst.next()) {
    Integer idProduto = rst.getInt(1);
    System.out.println("O ID criado foi: " + idProduto) ;
    }

}

}

Ecxeption:

Exception in thread "main" java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement(). at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.StatementImpl.getGeneratedKeys(StatementImpl.java:1376) at testaInsercao.main(testaInsercao.java:16)

1 resposta
solução!

Encontrei o erro a ecxeption aconteceu pois ao invez de chamar o metodo RETURN_GENERATED_KEYS eu chamei o NO_GENERATED_KEYS e isso causou o problema, quando fiz a chamada com o metodo correto rodou normalmente o teste.