int i=statement.executeUpdate("INSERT INTO CHARGE_T (CHA_FIRSTWEIGHT, CHA_FIRSTWPRICE, CHA_SECONDWPRICE, CHA_FIRSTVOLUME, CHA_FIRSTVPRICE, CHA_SECONDVPRICE, cha_status,cha_ran_name) VALUES ('"+chargeVO.getCha_firstweight()+"', '"+chargeVO.getCha_firstwprice()+"', '"+chargeVO.getCha_secondwprice()+"', '"+chargeVO.getCha_firstvolume()+"', '"+chargeVO.getCha_firstvprice()+"', '"+chargeVO.getCha_secondvprice()+"','未通过','"+chargeVO.getCha_ran_name()+"')");
int j=statement.executeUpdate("update range_t set ran_cha_id=(?) where cha_ran_name=ran_name");(?)那里怎么才能获得上面刚插入数据库的那条数据的id,求教

解决方案 »

  1.   

    1,CHARGE_T个表,不通过id ,其他那些字段能唯一标识这条记录就用这个做条件,然后查出id
    2,如果不通过id ,其他字段不能唯一标识,那你只能自己插入的时候给一个id ,这样你就可以知道你插入记录的id了
    3,直接查询你插入CHARGE_T表的当前sequence(不是很安全),
      

  2.   

    第三条,我可以用事务做,但是应该怎么获得sequence的值呢,求教
      

  3.   

    在你执行插入的同时去查询,当然你得知道你的序列的名字 假设你的序列叫 s_louzhu
    int i=statement.executeupdate("
    INSERT INTO CHARGE_T (CHA_FIRSTWEIGHT, CHA_FIRSTWPRICE, CHA_SECONDWPRICE, 
    CHA_FIRSTVOLUME, CHA_FIRSTVPRICE, CHA_SECONDVPRICE, cha_status,cha_ran_name)
    VALUES ('"+
    chargevo.getcha_firstweight()+"', '"+
    chargevo.getcha_firstwprice()+"', '"+
    chargevo.getcha_secondwprice()+"', '"+
    chargevo.getcha_firstvolume()+"', '"+
    chargevo.getcha_firstvprice()+"', '"+
    chargevo.getcha_secondvprice()+"','未通过','"+
    chargevo.getcha_ran_name()+"')");
    //这里的sq就是你刚才插入的序列的值
    int sq = statement.execute("select (s_louzhu.nextval-1) from dual");int j=statement.executeUpdate("update range_t set ran_cha_id="+sq +" where cha_ran_name=ran_name");
    希望楼主下回发帖的时候 在代码里适当的加入换行
      

  4.   

    int i=statement.executeUpdate("INSERT INTO CHARGE_T (CHA_FIRSTWEIGHT, CHA_FIRSTWPRICE, CHA_SECONDWPRICE, CHA_FIRSTVOLUME, CHA_FIRSTVPRICE, CHA_SECONDVPRICE, cha_status,cha_ran_name) VALUES ('"+chargeVO.getCha_firstweight()+"', '"+chargeVO.getCha_firstwprice()+"', '"+chargeVO.getCha_secondwprice()+"', '"+chargeVO.getCha_firstvolume()+"', '"+chargeVO.getCha_firstvprice()+"', '"+chargeVO.getCha_secondvprice()+"','未通过','"+chargeVO.getCha_ran_name()+"')");
    String sql1="update range_t set ran_cha_id=seq_charge_t.currval where '"+chargeVO.getCha_ran_name()+"'=range_t.ran_name";
    上面就解决了,谢谢了