I am one of those who looked through several threads looking for a solution to this problem ... and finally it worked. FOR THESE USES of jdbc: oracle: thin: with ojdbc6.jar PLEASE ACCEPT NOTE: You can use both methods: (Method 1)
Try{ String yourSQL="insert into Table1(Id,Col2,Col3) values(SEQ.nextval,?,?)"; myPrepStatement = <Connection>.prepareStatement(yourSQL, Statement.RETURN_GENERATED_KEYS); myPrepStatement.setInt(1, 123); myPrepStatement.setInt(2, 123); myPrepStatement.executeUpdate(); ResultSet rs = getGeneratedKeys; if(rs.next()) { java.sql.RowId rid=rs.getRowId(1); //what you get is only a RowId ref, try make use of it anyway U could think of System.out.println(rid); } } catch (SQLException e) { // }
(method 2)
Try{ String yourSQL="insert into Table1(Id,Col2,Col3) values(SEQ.nextval,?,?)"; //IMPORTANT: here where other threads don tell U, you need to list ALL cols //mentioned in your query in the array myPrepStatement = <Connection>.prepareStatement(yourSQL, new String[]{"Id","Col2","Col3"}); myPrepStatement.setInt(1, 123); myPrepStatement.setInt(2, 123); myPrepStatement.executeUpdate(); ResultSet rs = getGeneratedKeys; if(rs.next()) { //In this exp, the autoKey val is in 1st col int id=rs.getLong(1); //now this a real value of col Id System.out.println(id); } } catch (SQLException e) { // }
Basically, try not to use Method1, if you just need the value SEQ.Nextval, b'cse will just return a RowID ref so you can hack your search in your head to use it, which is also not suitable for all data types you tried casting! This may work fine (return actual val) in MySQL, DB2, but not in Oracle.
And, disable your SQL Developer, Toad, or any client that uses the same INSERT login session when you are debugging. It MAY NOT affect you every time (debugging call) ... until you find that your applications are frozen without any exceptions for some time. Yes ... without exception!
peterong Jun 24 '13 at 7:39 on 2013-06-24 07:39
source share