• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing auto increment programitcally

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I using mysql databse, i am implementing auto increament field programitically. Find the no of records in table and incrementing it by 1.
I want to this for "user_id field"


Each time when user adds a new record it will programtically increment user_id by 1 to no of records currently present in ts_user table






I am getting error when i excute query:






 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use an alias such as "select count(*) as the_sum ..." then you can call getInt("the_sum") to read it. In particular though, selecting count is a *really* bad practice for auto-incrementing data. First and foremost, if an old record is deleted, the entire system stops being able to insert records. Selecting the max id + 1 is generally better, but keep in mind none of these techniques are thread safe if you don't wrap them in a transaction. Two users both trying to insert records could collide if they both read the same id.
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your solution, it incrementing value by 1 but databse is getting upddated with different value.


Q How to implement transactions in follwoing code?



Tomcat console

tcount===2


databse value
104
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic