This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes getting last row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "getting last row" Watch "getting last row" New topic
Author

getting last row

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

hi
Can any one tell me query for getting last row from the table.
Thanks


-Chiru
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There are a variety of ways you can do this, all of which are DB dependent. Which database are you using?
[ July 18, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

MySQL
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you are using InnoDB you will have a clustered index on your primary key column. The ordering of your clustered index will match the physical ordering of your data, so the maximum primary key will be the last row. Soemthing like:

will get you that.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

But my primary key is Date time Stamp i think this max wont work with date time stamp
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Chiru Raj:
But my primary key is Date time Stamp i think this max wont work with date time stamp


Why not? Max should returns the latest date; this is how it behaves on most DBs. Is MySQL different?
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

ok thank you.
i will check that and tell you.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3740
    
  10

Just wondering, but is the purpose of your query to retrieve a row id (or other information) immediately after it's inserted? If so there is a getGeneratedKeys() command that will return the primary key of any records created by a statement. Of course support for this feature is system dependent so test it out before assuming this works. The other approach to get the id of the keys recently inserted without relying on the database is to create the ids of the keys yourself (instead of using the database's key generation feature). In large enough systems where concurrency is an issue, this is often the best approach.
[ July 18, 2008: Message edited by: Scott Selikoff ]

[OCA 8 Book] [Blog]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44047
    
  33
I think in MySQL you use the last_insert_id function for what Scott suggested. Check the documentation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44047
    
  33
. . . but last_insert_id only works where you have an auto_increment column.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

Originally posted by Paul Sturrock:




Error is coming

my query is


[ July 19, 2008: Message edited by: Scott Selikoff ]
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

This is working fine
 
Have you checked out Aspose?
 
subject: getting last row
 
It's not a secret anymore!