列的索引超出范围:2,列数1

时间:2021-05-07 16:41:03

I have this tabel :

我有这个表格:

Klas_student

Klas_student

CREATE TABLE IF NOT EXISTS Klas_student(
Student varchar(7) REFERENCES studenten (Studentenummer) ON DELETE CASCADE NOT NULL,
Klas text NOT NULL REFERENCES Klas (Naam_id) ON DELETE CASCADE NOT NULL
);

In this tabel i want to add values, i do that this way with preparedstatement.

在这个tabel我想添加值,我这样做与预备声明。

PreparedStatement studentToKlas = conn.prepareStatement("INSERT INTO Klas_student " + "VALUES (?)");
                studentToKlas.setString(1, studentnummer);
                studentToKlas.setString(2, klasIdToInsert);

However this error keeps popping up :

但是这个错误不断出现:

org.postgresql.util.PSQLException: L'indice de la colonne est hors limite : 2, nombre de colonnes : 1.
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:56)
at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:118)
at org.postgresql.jdbc2.AbstractJdbc2Statement.bindString(AbstractJdbc2Statement.java:2304)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1392)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setString(AbstractJdbc2Statement.java:1374)
at PerformanceClass$1.run(PerformanceClass.java:73)
at java.lang.Thread.run(Thread.java:724)

It basically says that the index of the columnis beyond limit : 2, and number of columns is one.

它基本上表示列的索引超出限制:2,列数为1。

PerformanceClass.java:73 is this line of code :

PerformanceClass.java:73就是这行代码:

 studentToKlas.setString(2, klasIdToInsert);

As you can see Klas_student has two fields, so i don't really understand the error. Does any one of you see what i am doing wrang?

正如你所看到的,Klas_student有两个字段,所以我真的不明白错误。你们其中任何一个人看到我在做什么吗?

3 个解决方案

#1


2  

You have two columns, so your statement should be :

你有两列,所以你的陈述应该是:

"INSERT INTO Klas_student VALUES (?, ?)")

i.e. it should contain two placeholders, one for each column.

即它应该包含两个占位符,每列一个占位符。

#2


1  

It is because you are binding only 1 parameter here:

这是因为你在这里只绑定了一个参数:

("INSERT INTO Klas_student " + "VALUES (?)")

Change it to:

将其更改为:

 ("INSERT INTO Klas_student " + "VALUES (?,?)")

#3


1  

This is your statement:

这是你的陈述:

INSERT INTO Klas_student " + "VALUES (?)

The table Klas_student has two columns. So, this is equivalent to:

表Klas_student有两列。所以,这相当于:

INSERT INTO Klas_student(Student, Klas) " + "VALUES (?)

Now, you can clearly see what the error is. There are two columns, you are inserting one. In your case, both columns are not-NULL, so you need to put in two values:

现在,您可以清楚地看到错误是什么。有两列,您正在插入一列。在您的情况下,两列都不是NULL,因此您需要输入两个值:

INSERT INTO Klas_student(Student, Klas) " + "VALUES (?, ?)

The reason I'm adding an answer, though, is to emphasize that you should always include a column list when doing INSERT. Do not assume the number or ordering of the values. It might make sense to you when you write the code. It won't be apparent what you are doing two weeks later. It won't make sense to anyone else reading the code. Be explicit.

我添加答案的原因是强调在执行INSERT时应始终包含列列表。不要假设值的数量或顺序。编写代码时可能对您有意义。两周之后你会做什么并不明显。阅读代码的任何人都没有意义。要明确。

#1


2  

You have two columns, so your statement should be :

你有两列,所以你的陈述应该是:

"INSERT INTO Klas_student VALUES (?, ?)")

i.e. it should contain two placeholders, one for each column.

即它应该包含两个占位符,每列一个占位符。

#2


1  

It is because you are binding only 1 parameter here:

这是因为你在这里只绑定了一个参数:

("INSERT INTO Klas_student " + "VALUES (?)")

Change it to:

将其更改为:

 ("INSERT INTO Klas_student " + "VALUES (?,?)")

#3


1  

This is your statement:

这是你的陈述:

INSERT INTO Klas_student " + "VALUES (?)

The table Klas_student has two columns. So, this is equivalent to:

表Klas_student有两列。所以,这相当于:

INSERT INTO Klas_student(Student, Klas) " + "VALUES (?)

Now, you can clearly see what the error is. There are two columns, you are inserting one. In your case, both columns are not-NULL, so you need to put in two values:

现在,您可以清楚地看到错误是什么。有两列,您正在插入一列。在您的情况下,两列都不是NULL,因此您需要输入两个值:

INSERT INTO Klas_student(Student, Klas) " + "VALUES (?, ?)

The reason I'm adding an answer, though, is to emphasize that you should always include a column list when doing INSERT. Do not assume the number or ordering of the values. It might make sense to you when you write the code. It won't be apparent what you are doing two weeks later. It won't make sense to anyone else reading the code. Be explicit.

我添加答案的原因是强调在执行INSERT时应始终包含列列表。不要假设值的数量或顺序。编写代码时可能对您有意义。两周之后你会做什么并不明显。阅读代码的任何人都没有意义。要明确。