Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

320
Visualizações
What is the difference between these two sql statements(JDBC)?

I want to get last id in table. First I did this:

String selectId = "SELECT MAX(id) FROM book_store.cart" ;    
PreparedStatement ps = conn.prepareStatement(selectId);  
rs =  ps.executeQuery();
while(rs.next()){
    autoIncKey = rs.getInt("id");
}

Here I get error "column id not not found".

Then I changed the query to this, which is working:

String selectId = "SELECT id FROM book_store.cart ORDER BY id DESC LIMIT 1";

I am wondering why the first query is giving me an error and second does not if they are returning the same value?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The first query will return a row with a column name of MAX(id).

Not the most useful name for a column, so change the query to set an alias for that column like this

SELECT MAX(id) id FROM book_store.cart

And now the column will be called id and the rs.getInt("id") will work

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda