Solucionado (ver solução)
Solucionado
(ver solução)
10
respostas

getInt(String) não funciona no exercício

Naõ consegui rodar o exercício acessando pelo nome da coluna.

Veja o código

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 criaconexao = new ConnectionFactory();
        Connection con = criaconexao.recuperaConexao();

        Statement stm = con.createStatement();
        stm.execute("INSERT INTO PRODUTO (nome,descricao) VALUES ('CANETA','ESFEROGRAFICA')",Statement.RETURN_GENERATED_KEYS);
        //stm.execute("SELECT ID,NOME,DESCRICAO FROM PRODUTO");
        ResultSet rst = stm.getGeneratedKeys();
        while(rst.next()) {
            int id = rst.getInt(1);
//            int id = rst.getInt("id");
//            String nome = rst.getString("nome");
//            String descricao = rst.getString("descricao");
//            System.out.printf("Dado Inserido: %n%s | %s | %s %n",id,nome,descricao);
            System.out.println(id);    
    }

}
}
10 respostas

Maurício, boa tarde. Você vê o produto inserido no banco de dados?

Não. Ele da erro na instrução getInt

Se vc fizer o getInt("ID"), funciona?

Não. Esse é o ponto. Só funciona com argumento numérico.

Ah, agora entendi o ponto. Engraçado, a coluna é id todo minúsculo mesmo? Da qual erro quando você usa a string?

Não importa se escrevo maiúscula ou minúscula. Ele diz que o campo é desconhecido. A mesma instrução é utilizada no TesteListagem sem erro

Oi Mauricio, tem como postar o erro / stacktrace que apareceu na hora de executar?

obrigado, Nico

Aí está Nico.

mar. 08, 2021 6:50:03 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
mar. 08, 2021 6:50:03 PM com.mchange.v2.c3p0.C3P0Registry 
INFO: Initializing c3p0-0.9.5.4 [built 23-March-2019 23:00:48 -0700; debug? true; trace: 10]
mar. 08, 2021 6:50:03 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge10jag5hyn0s1min71e|52525845, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge10jag5hyn0s1min71e|52525845, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost/loja_virtual?useTimezone=true&serverTimezone=UTC, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {password=******, user=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
Exception in thread "main" java.sql.SQLException: Column 'id' not found.
    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.result.ResultSetImpl.findColumn(ResultSetImpl.java:548)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:807)
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.getInt(NewProxyResultSet.java:451)
    at teste.TestaInsercao.adicionaDado(TestaInsercao.java:37)
    at teste.TestaInsercao.main(TestaInsercao.java:25)

Se eu substituir o parametro string pelo numero da coluna equivalente funciona o código

solução!

Oi Mauricio, nessa questão estou um pouco engatinhando ... mas achei uma breve explicação nessa post no StackOverflow:

https://stackoverflow.com/questions/13205031/jdbc-getgeneratedkeys

Citando a resposta: *Some databases (or drivers) can't easily decide what is the actual generated column. For example because the database doesn't have IDENTITY columns, but use triggers to generate keys. Identifying generated keys would involve parsing all triggers on a table to check if it assigns a generated value to a (primary key or other) column, which is not easily done and would be error prone. And sometimes there are multiple generated columns (ie computed fields etc). You as the developer should know what fields you can or want to get back.

As it is hard (or inefficient) to decide which fields to return, some drivers (by default) return all columns of the inserted (or deleted/updated) row. For example the PostgreSQL and Firebird drivers do that. On the other hand some drivers just return the last-generated key even if the table does not contain an identity column (I believe MySQL does, not 100% sure though). And I seem to remember that the Oracle driver simply returns the ROWID, leaving it up to the user to retrieve actual values from the database using that ROWID.*

Ou seja, o comportamento varia de driver para driver e nao parece ser garantido que o getInt(String) funciona. Eu só achei exemplos que usam getInt(1) ou getLong(1) inclusivo o Alberto menciona isso nesse post:

https://cursos.alura.com.br/forum/topico-buscando-id-com-getgeneratedkeys-69317

abs

Obrigado Nico.

Esta observação deveria fazer parte do video, afimal, gastei um bom tempo procurando um erro que não era meu.

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