0

I am trying to insert the data in the Oracle Database table by using the java.sql.PreparedStatement. I am, however, not able to get the autogenerated keys. The issue here is after running the

statement.executeUpdate();

I am getting the exception saying the col1 cannot be null. Isn't it supposed to generate the col1 value and upload it in the database? Not sure what I am missing here.

I am new to the JDBC implementation so seeking help. Thank you.

col1: column to store auto-generated ID. col2 and col3 to store other data.

DAO:

 PreparedStatement statement;

 String query ="INSERT INTO table("col2", "col3") VALUES(?,?)";
 statement = getConnection().prepareStatement(query, new String[]{ "col1" });
 //add values to the statement for these two columns(col2 and col3) for this row in this step
 statement.executeUpdate();

The database column to store the ID is non-nullable and is not set to autoincrement.

user1435077
  • 49
  • 1
  • 6
  • What is this `col1` you are passing to `prepareStatement`. This is likely your problem, because that method expects either `Statement.RETURN_GENERATED_KEYS` or `Statement.NO_GENERATED_KEYS`, and I think it is unlikely that is what `col1` has as value. If you want to obtain the value of the column `col1`, then use `prepareStatement(query, new String[] { "col1" })` – Mark Rotteveel May 08 '20 at 19:01
  • Yes I am using prepareStatement(query, new String[] { "col1" }) – user1435077 May 08 '20 at 19:08
  • If the column `col1` is not nullable, and not auto-increment (or backed by a trigger with sequence), why do you expect this to work? In that case you need to provide a value for the column yourself as nothing is generated, as the error also indicates. – Mark Rotteveel May 09 '20 at 06:01

0 Answers0