permaculture playing cards*
The moose likes JDBC 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getting last row" Watch "getting last row" New topic
Author

getting last row

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

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: 289

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: 289

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: 289

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

Joined: Oct 23, 2005
Posts: 3703
    
    5

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 ]

My Blog: Down Home Country Coding with Scott Selikoff
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38031
    
  22
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: 38031
    
  22
. . . but last_insert_id only works where you have an auto_increment column.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

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: 289

This is working fine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting last row
 
Similar Threads
JSP Invoking another JSP in Iframe
Where's the backslash?
Deleting Row from JTable
Resultset
First and Last Row numbers from viewable portion of jtextarea?