Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

319
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda