Java 错误:列索引超出范围:1,列数:0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36439305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:53:31  来源:igfitidea点击:

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

javapostgresqlinsert

提问by Nesan Mano

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

我正在尝试解决插入 Postgresql 表的问题

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

错误:列索引超出范围:1,列数:0

here is the part of code getting the error:

这是出现错误的代码部分:

String query = "INSERT INTO reviews (nbstar, body, author, product_id) VALUES(,,,)";

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.

谢谢。

采纳答案by Tran Ho

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).

在 sql 查询中,您想插入 5 个字段(id、nbstar、body、author、product_id)的值,但只有 4 个值 VALUES($1,$2,$3,$4)。



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

按照您编辑的问题进行更新,只需按如下方式修改您的查询:

VALUES(,,,) 

to

VALUES(?,?,?,?)