java.lang.ArrayIndexOutOfBoundsException: 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12569049/
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
java.lang.ArrayIndexOutOfBoundsException: 2
提问by user1694994
I'm trying to connect java to sql. Though I'm a newbie with javam I can't seem to find out why.
我正在尝试将 java 连接到 sql。虽然我是 javam 的新手,但我似乎无法找出原因。
java.lang.ArrayIndexOutOfBoundsException: 2
Can you help me and give me idea how to figure it out?
你能帮助我并告诉我如何解决这个问题吗?
try{
Connection con = dbConnection();
String lname = this.last.getText();
String fname = this.first.getText();
String mname = this.mid.getText();
String ad = this.add.getText();
String bd = this.bday.getText();
String ag = this.edad.getText();
String nom = this.no.getText();
String per = this.person.getText();
String query = "INSERT INTO Personal Category (Lastname,Firstname,Middle,Address,Birthday,Age,No,Person) VALUES (?,?)"; //to insert to database
PreparedStatement pre;
pre = con.prepareStatement(query);
pre.setString(1, lname);
pre.setString(2, fname);
pre.setString(3, mname);
pre.setString(4, ad);
pre.setString(5, bd);
pre.setString(6, ag);
pre.setString(7, nom);
pre.setString(8, per);
pre.execute();//execute
con.close();
}catch (Exception e){
System.out.println(e);
}
}
回答by Denys Séguret
You cannot set more prepared statement properties than you have '?' in your query.
您不能设置比“?”更多的准备好的语句属性。在您的查询中。
"ArrayIndexOutOfBoundsException: 2" means it fails at the third setString (the internal array is zero-based as all java arrays), which is normal given you have only two '?' in the query.
"ArrayIndexOutOfBoundsException: 2" 表示它在第三个 setString 失败(内部数组与所有 java 数组一样从零开始),这是正常的,因为您只有两个 '?' 在查询中。
EDIT :
编辑 :
You probably have an error due to the space in your table name. Try with
由于表名中的空格,您可能有错误。试试
INSERT INTO [Personal Category] (Lastname, ...
回答by asteri
You only have two ?
in your VALUES()
declaration. You need to have as many as you have column names.
您?
的VALUES()
声明中只有两个。您需要拥有与列名一样多的数量。
Try
尝试
String query = "INSERT INTO Personal Category (Lastname,Firstname,Middle,Address,Birthday,Age,No,Person) VALUES (?,?,?,?,?,?,?,?)"; //to insert to database
回答by dasblinkenlight
You need more question marks - six more, to be precise.
你需要更多的问号——准确地说,还有六个问号。
String query = "INSERT INTO Personal Category (Lastname,Firstname,Middle,Address,Birthday,Age,No,Person) VALUES (?,?,?,?,?,?,?,?)";
JDBC interprets each question mark as a placeholder for a parameter, i.e. a promise by your program to supply a value after preparing the statement. When you call setString
, setInt
, setLong
, etc. on your prepared statement, a corresponding numbered placeholder must exist. Otherwise, java.lang.ArrayIndexOutOfBoundsException
is thrown.
JDBC 将每个问号解释为参数的占位符,即您的程序承诺在准备语句后提供一个值。当您在准备好的语句上调用setString
、setInt
、setLong
等时,必须存在相应的编号占位符。否则,java.lang.ArrayIndexOutOfBoundsException
被抛出。
回答by Vikdor
You should have as many placeholders (?
) as the number of parameters to be passed to that INSERT statement.
您应该拥有与?
要传递给该 INSERT 语句的参数数量一样多的占位符 ( )。
String query = "INSERT INTO PersonalCategory (Lastname, Firstname, Middle, " +
"Address, Birthday, Age, No, Person) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
回答by Kumar Vivek Mitra
String query = "INSERT INTO Personal Category (Lastname,Firstname,Middle,Address,Birthday,Age,No,Person) VALUES (?,?)";
String query = "INSERT INTO Personal Category (Lastname,Firstname,Middle,Address,Birthday,Age,No,Person) VALUES (?,?)";
The problem is with the Number of arguments to be passed in the VALUES, You have passed only 2 arguments where it needs to be 8.
问题在于要在 VALUES 中传递的参数数量,您只传递了 2 个需要为 8 的参数。
回答by Lemon Juice
you have got the answer to "ArrayIndexOutOfBound" issue. I will answer to your second question "SQLException" and give some advice to improve your code.
你已经得到了“ArrayIndexOutOfBound”问题的答案。我将回答您的第二个问题“SQLException”并提供一些建议以改进您的代码。
That SQLException is happening because your table name I guess. There are 2 words in your table name and that might the confusing the compiler.
那个 SQLException 正在发生,因为我猜你的表名。您的表名中有 2 个单词,这可能会使编译器感到困惑。
Advices
建议
you are executing preparedStatement without catching its output value. It will give you an boolean value as the output. Then only you can find what kind of thing has been executed.
In here, it seems like you are ONLY INSERTING data, and you are sure you are ONLY INSERTING DATA. So, if it is, use executeUpdate() rather than execute() method.
Always close the connection inside a final() block. This way, you can always make sure the connection is closed whether the code fails or not. By doing this, Database rush will be managed smoothly, by not keeping unnecessary connections open.
您正在执行 PreparedStatement 而不捕获其输出值。它会给你一个布尔值作为输出。那么只有你才能找到执行了什么样的事情。
在这里,您似乎只是在插入数据,并且您确定您只是在插入数据。因此,如果是,请使用 executeUpdate() 而不是 execute() 方法。
始终关闭 final() 块内的连接。这样,无论代码是否失败,您始终可以确保连接已关闭。通过这样做,数据库高峰将得到顺利管理,不会保持不必要的连接打开。
Lets stay with good software engineering concepts :)
让我们保持良好的软件工程概念:)