java - How do I insert last_insert_id using preparedStatement? -
i facing difficulty in inserting " last_insert_id" in prepared statement.i got how select last_insert_id in prepared statement below:
preparedstatement getlastinsertid = con.preparestatement("select last_insert_id()");
when use same procedure inserting last_insert_id in preparedstatement this:
1. preparedstatement pst = con.preparestatement("insert introducer_table values(?,?,?,?)"); 2. 3. //introducer details database 4. pst.setstring(1,last_insert_id()); 5. pst.setstring(2, nameofintroducer); 6. pst.setstring(3, accountno); 7. pst.setstring(4, signofintroducer);
im getting 'null' value in first column.can 1 me come out problem
if doing both save actions @ time use getgeneratedkeys()
, it's pretty java.
i'm not sql guru, here found way generated id using getgeneratedkeys()
long generatedid= 0l; statement = con .getconnection() .preparestatement( "insert new_user set name= ? , contact= ? , ....", statement.return_generated_keys); statement.setstring(1, "examplename"); statement.setstring(2, "examplecontact"); ------ statement.executeupdate(); resultset generatedkeys = statement.getgeneratedkeys(); if (generatedkeys.next()) { generatedid = generatedkeys.getlong(1);// here generated id , use insert in introducer_table } preparedstatement pst = con.preparestatement("insert introducer_table values(?,?,?,?)"); //introducer details database pst.setstring(1, generatedid); pst.setstring(2, nameofintroducer); pst.setstring(3, accountno); pst.setstring(4, signofintroducer);
Comments
Post a Comment