0
votes

below is some part of my java code which we are using to insert record into a table.

public void insertitem(long item_id, long user_id) throws SQLException
   {
     PreparedStatement pstmt = null;
      StringBuffer sqlB = new StringBuffer();


      sqlB.append("INSERT INTO ITEMS ");
      sqlB.append("(AIR_ID, ACT_ITEM_ID, USER_ID) ");
      sqlB.append("  VALUES ");
      sqlB.append("(ITEMS_SEQ.nextval,?,?) ");
      try
      {
         pstmt = conn.prepareStatement(sqlB.toString());
         int i=1;
         pstmt.setLong(i, item_id);i++;
         pstmt.setLong(i, user_id);i++;
         pstmt.executeUpdate();

      }
      catch(SQLException e)
      {
         throw new SQLException(e.getMessage());
      }
}

even though iam using SEQ.nextval, it still throwing the below error:

java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (ACTTRK.AIR_ID_PK) violated.**

how can i resolve this ?

1
Are you implying that column AIR_ID is the primary key of database table ITEMS? - Abra
Seems to me you have to adjust the current value of the sequence according to the max(air_id) in the table - Turo
Have a table definition that takes care of this. GENERATED - jarlh
@Turo how can i set the sequence to max(AIR_ID) - CHILUKA BHARATH
@Abra yes, AIR_ID is primarykey of table ITEMS - CHILUKA BHARATH

1 Answers

1
votes

Set the current sequence value with

alter sequence ITEMS_SEQ restart start with <your_max_value+1>;  

If this doesn't work

ALTER SEQUENCE ITEMS_SEQ INCREMENT BY <your_max_value>;

select ITEMS_SEQ.nextval from dual;

ALTER SEQUENCE ITEMS_SEQ INCREMENT BY 1;