Search code examples
javapostgresqlinsert

error: The column index is out of range: 1, number of columns: 0


I'm trying to solve the problem of doing an insert into a Postgresql table

I looked at this similar question but it did not solve my problem

ERROR : The column index is out of range: 1, number of columns: 0

here is the part of code getting the error:

String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES($1,$2,$3,$4)";

PreparedStatement prepareStatement = connection.prepareStatement(query);
prepareStatement.setInt(1, nbStar);
prepareStatement.setString(2, body);
prepareStatement.setString(3, author);
prepareStatement.setInt(4, productId);

boolean executed = prepareStatement.execute();

i tried several times to change the index number but still the same error

and here is the schema of the table:

table schema

can anyone give me an advice ?

thanks.


Solution

  • In the sql query, you want to insert the values for 5 fields (id, nbstar, body, author, product_id) but there are only 4 values VALUES($1,$2,$3,$4).


    Update following your edited question, just modify your query as follows:

    VALUES($1,$2,$3,$4) 
    

    to

    VALUES(?,?,?,?)