Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!