473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how to get ms access 2007 auto increment id in java

i'm using the access db for this example.i want to get the last record auto increment id

Expand|Select|Wrap|Line Numbers
  1. package com.java2novice.jdbc;
  2. *
  3. import java.sql.*;
  4.  
  5. *
  6. public class MyAutoGeneratedKeys {
  7. *
  8. public static void main(String a[]){
  9. ********
  10. Connection con = null;
  11. PreparedStatement pstmt = null;
  12. ResultSet rs = null;
  13. try {
  14. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  15. con = DriverManager.getConnection("jdbc:odbc:student");
  16. String query = "insert into Student(fname,lname,gender) values (?,?,?)";
  17. *********
  18. pstmt = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
  19. pstmt.setString(1, "John");
  20. pstmt.setString(2, "methev");
  21. pstmt.setInt(3,"Male");
  22. ***********
  23. pstmt.executeUpdate();
  24. ************
  25. rs = pstmt.getGeneratedKeys();
  26. ************
  27. while(rs.next()){
  28. System.out.println("Generated Emp Id: "+rs.getObject(1).toString());
  29. }
  30. *
  31. }  catch (SQLException e) {
  32. **********
  33. } finally{****}
  34. ****
  35. *
  36. }
  37. }
Feb 22 '14 #1
1 3019
chaarmann
785 Expert 512MB
You can't get the next free autoincrement number (aliaas ID), it's a database issue and has to do with concurrency of requests.
Method 1: let's say you would get the next free number somehow (for example by asking for the current maximum of the id-column, then you add 1 to it) and use this for the new record you want to insert. In the meantime another process could have done the same and already inserted the new entry. Then when you try to insert your entry, you get back an error: ID already used!

So the only way I know is, to insert a record first and then ask the JDBC-interface for the inserted ID. For example for MySQL:

Expand|Select|Wrap|Line Numbers
  1. Statement statement = connection.createStatement(Statement.CLOSE_ALL_RESULTS, Statement.RETURN_GENERATED_KEYS);
  2. statement.execute("insert into table ...");
  3. ResultSet generatedKeys = statement.getGeneratedKeys();
  4. generatedKeys.next();
  5. int autoIncrementedId =generatedKeys.getInt(1);
You could insert an entry with default values first and later use "update" with its ID to update it.
Mar 10 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: diong | last post by:
i have a field with a list of wrong ID (random) and i wish to replac them with a sequential (auto-increment) value. how to remove the valu and to add in the sequential ID? Im a newbie in ASP! Pls...
3
by: Yan Roosens | last post by:
Hello all, I'm a total newbie with SQL Server 2000 and I have a little problem when moving a database form Access 2000 to SQL Server 2000. In the Access database, each table has an...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
4
by: JO | last post by:
hello, how could i make to know if my column PK on access is auto increment. with .net i use Dim dtLstKP As DataTable = MyConn.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New Object()...
1
by: rhepsi | last post by:
hii all, i have created a database with a table with number of fields....in msaccess ive set primary key as SNo: and its datatype as auto number... How to set the auto increment ???? ...
1
by: linktoali | last post by:
hi i want to increment the no as a autonumber in the rows of composit key in my MS Access data base kindly send me the VB code for doing so.bcause i want to incremnt the no of rows auto number in...
0
by: umutesi | last post by:
I am developping a Database in access.I am tyring to make an access interface that will make a lot of things easier .Now I have a text field which is an Id for my table and it has ...
2
by: Geby | last post by:
any one can help? how to create auto increment ID which is combined from one or more field. example: ID....? Name : Shadow Age : 28 years old after i insert data to field Name and Age, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.