Re: how can i move to the last record of recordset



"ashwinijain" <ashwinijain27@xxxxxxxxx> wrote in
news:1165238889.171879.179980@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

i have one table in MS. Access in which i am storing company details.
i want to create an unique company id for each company.
so i want to aceess the last row in the table to generate next id
I am using last() method but its throwing sqlexception.
rs=stat1.executeQuery("Select Company_id from Company ");

// Code
if(rs!=null)
{
rs.last();
no=rs.getInt(1) + 1;
}
else
no=1;

Hi,

Isn't that a bit overkill? You're (possibly) selecting a lot of records,
just to get the last one.

It's better to perform a "select max(company_id) from company" and work
from there - this way you'll avoid retrieving a huge resultset.

Please keep in mind that this is not a safe method: what happens when two
threads perform the same thing? They both select the same max value, add
one to said value, and then one of them will try to insert a duplicate
key!

In Oracle, I've always used sequences - Access has some sort of auto
increment field, IIRC. It's probably better to use that feature.

Best regards,

JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
.