Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / December 2006

Tip: Looking for answers? Try searching our database.

how can i move to the last record of recordset

Thread view: 
ashwinijain - 04 Dec 2006 13:28 GMT
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;
jcsnippets.atspace.com - 05 Dec 2006 13:18 GMT
>  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.
[quoted text clipped - 10 lines]
> 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
Signature

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

Lew - 05 Dec 2006 18:51 GMT
> In Oracle, I've always used sequences - Access has some sort of auto
> increment field, IIRC. It's probably better to use that feature.

The use of auto-increment fields as artificial keys is controversial. Some DB
gurus excoriate the practice in favor of semantically valid keys.

Even if you favor it, there are considerations, such as coordinating values
through an unload and reload of the data. Be careful.

- Lew
Till - 17 Dec 2006 10:30 GMT
Am Mon, 04 Dec 2006 05:28:09 -0800 schrieb ashwinijain:

>  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.
[quoted text clipped - 10 lines]
> else
>  no=1;

you could use  "order by company_id asc" in the sql-statement and take
the first row of the resultset.

concerning the Exception: are there any records in the resultset ?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.