• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

MAX ID

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create id .
My code is ....
public static String getID()
{
try{
String qr="select max(id) from user where id like 'EMS%'";
Connection con....
Ststement st....
rs=st.executeQuery(qr);
while(rs.next()){
if(rs.getString(1)=="" ||rs.getString(1)==null)
{total_id="EMS0000";
return total_id;
}
else
{
id=id.substring(3,4);
total_id="EMS"+(Integer.parseInt(id)+1);
return total_id;
}
}//while
}catch(E.....){}
return total_id;
}//end function


NOTE:----
IN DATA BASE id= EMS0009,EMS0010......THEN MAX(ID) Return EMS0009.......
I want to EMS0010.......PL z HELP ME .

 
author & internet detective
Posts: 40196
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What database are you using?

I tried the following in postgreSQL and got EMS0010 returned. It's using an alphabetic sort rather than a numeric one, but that's ok if you always have the same number of digits. (leading zeros)
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code in a code block so it is easier to read.

I suspect you are getting the wrong ID back because of alphabetic sorting (9 is greater than 10) As Jeanne said, can you double-check to make sure
your data isn't like this:
EMS009
EMS0010

instead of:
EMS0009
EMS0010
 
    Bookmark Topic Watch Topic
  • New Topic