Home arrow static arrow Java Programming [Archive] - can you SELECT the last row?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - can you SELECT the last row?
This topic has 4 replies on 1 page.

Posts:343
Registered: 7/13/04
can you SELECT the last row?  
Jul 30, 2004 2:49 PM



 
I was wondering if there is a way to select the last row, or current row, or something of the sort. The reason i need to do this is i can't

"SELECT id FROM framedata WHERE (bunch of values here) " // id is the primary key

because there are multiple instances of the "bunch of values", so i just wanted to select the current row, or last row, or something like that.
 

Posts:31,095
Registered: 4/30/99
Re: can you SELECT the last row?  
Jul 30, 2004 2:53 PM (reply 1 of 4)



 
So that query gives you a set of records and you want to get the last one of them? Since it produces an ordered set, it must have an ORDER BY clause. Change that to read "ORDER BY ... DESC" and you will get them in the reverse order. Then the first record will be the one you want.

Or perhaps that wasn't your question. You may have meant something else, I suppose, but "last row" and "current row" don't mean anything unless you have a result set for those terms to be about. So I assumed your question was about a result set. If not, try clarifying.

PC²
 

Posts:343
Registered: 7/13/04
Re: can you SELECT the last row?  
Jul 30, 2004 3:01 PM (reply 2 of 4)



 
yes, i meant result set, for example this is what my code does:

-first -inserts data into different columns in the table

-second - obtain the primary key (which is randomly generated) of the row i just inserted into the table

-third- put a blob into that row (which is identified by the primary key)

its easy to put stuff into a table, and i also now have the blob working. but my program doesn't know where to put the blob because i need the primary key. (the column is already made for the blob) i was trying to use result set, so that i can query for the primary key (useing WHERE A = B AND C = D AND E = F) but i realized that some rows have the exact same values (except the primary key) i hope this explanation is a little clearer Dr. Clap.
 

Posts:3,055
Registered: 18/06/98
Re: can you SELECT the last row?  
Jul 30, 2004 3:12 PM (reply 3 of 4)



 
If you have a JDBC 3.0 driver for your database you can try the

From the JDBC 3.0 spec:

Summary of New Features:
...
Retrieval of auto-generated keys
Added a means of retrieving values from columns containing automatically
generated values.
...
13.6 Retrieving Auto Generated Keys
Many database systems have a mechanism that automatically generates a unique
key field when a row is inserted. The method Statement.getGeneratedKeys,
which can be called to retrieve the value of such a key, returns a ResultSet object
with a column for each automatically generated key. A flag indicating that any auto
generated columns should be returned is passed to the methods execute,
executeUpdate or prepareStatement when the statement is executed or
prepared.

Statement stmt = conn.createStatement();    // indicate that the key generated is going to be returned    int rows = stmt.executeUpdate("INSERT INTO ORDERS " +        "(ISBN, CUSTOMERID) " +        "VALUES (195123018, �BILLG�)",        Statement.RETURN_GENERATED_KEYS);    ResultSet rs = stmt.getGeneratedKeys();    boolean b = rs.next();    if (b == true) {        // retrieve the new key value        ...    }

CODE EXAMPLE 13-28 Retrieving auto generated keys

Additional methods allow the ordinals or names of the columns that should be
returned to be specified. In CODE EXAMPLE 13-29 the Statement method
executeUpdate is called with two parameters, the first is the SQL statement to be
executed, the second is an array of String containing the column name that should
be returned when getGeneratedKeys is called:
String keyColumn[] = {"ORDER_ID"};...Statement stmt = conn.createStatement();int rows = stmt.executeUpdate("INSERT INTO ORDERS " +String keyColumn[] = {"ORDER_ID"};...Statement stmt = conn.createStatement();int rows = stmt.executeUpdate("INSERT INTO ORDERS " +"(ISBN, CUSTOMERID) " +"VALUES (966431502, �BILLG�)",keyColumn);ResultSet rs = stmt.getGeneratedKeys();....

CODE EXAMPLE 13-29 Retrieving a named column using executeUpdate and
getGeneratedKeys
See the API Specification for more details.
 

Posts:27,518
Registered: 11/3/97
Re: can you SELECT the last row?  
Jul 30, 2004 4:23 PM (reply 4 of 4)



 
If you have a JDBC 3.0 driver for your database you
can try the

Alternatively you can search the JDBC forum where the same question has been asked and answered a number of times.
 
This topic has 4 replies on 1 page.