Connection con = getDBConnection(); Statement stmt = con.prepareStatement("Your SQL Query, Statement.RETURN_GENERATED_KEYS"); stmt.setString(1,"Input"); if(stmt.executeUpdate()>0) { ResultSet keySet=stmt.getGeneratedKeys(); if(keySet!=null && keySet.next()) { id = keySet.getLong(1); keySet.close(); } }
Monday, 22 June 2015
How to get the generated sequence/key after the insertion into DB
There is a table with multiple columns where one of the columns is an ID, which indicates the running sequence number. When a row is inserted into the Database, the DB will generate the sequential number and insert it. When the same operation is achieved through Java program and you want to use the generated sequence number for the consequence process in Java, you need to write the following code.
Labels:
Java,
SQL Server
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment