JDBC PreparedStatement.setString 抛出越界异常
JDBC PreparedStatement.setString throws Out Of Bounds exception
我有一个像这样的简单 PreparedStatement 字符串:
INSERT INTO albums (name, date) VALUES (?, ?)
我用这个创建了 table:
CREATE TABLE "albums" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"date" TEXT NOT NULL
);
这是我能写的最简单的例子:
public static void main(String[] args)
Connection dbConnection;
String sqlAlbumQuery = "INSERT INTO albums (name, date) VALUES (?, ?)";
PreparedStatement statement;
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:plswork.sqlite");
}
catch (SQLException e) {
System.err.println("Failed to establish connection: " + e);
return;
}
try {
statement = dbConnection.prepareStatement(sqlAlbumQuery);
statement.setString(0, "some album"); //StatementTest.java:40
statement.setString(1, "2012"); //StatementTest.java:41
statement.executeUpdate();
}
catch (SQLException e) {
System.err.println("Failed to create album: " + e);
}
}
这是我收到的消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:130)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setString(JDBC3PreparedStatement.java:417)
at statementtest.StatementTest.main(StatementTest.java:40)
C:\Users\notangryatall\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\notangryatall\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:94: Java returned: 1
我将 Netbeans 11.3 与 JDK 1.8 和 sqlite-jdbc-3.30.1.
一起使用
我尝试更改为 JDK 13,尝试使用旧的 sqlite-jdbc-3.23.1,尝试在 sqlAlbumQuery
的末尾添加分号,尝试使用 statement.executeQuery()
而不是 statement.executeUpdate()
并尝试 Google 但仍然找不到任何东西。
问题在这里:
statement.setString(0, "some album"); //StatementTest.java:40
准备语句索引从1开始(来自PreparedStatement接口):
@param parameterIndex the first parameter is 1, the second is 2, ...
the first parameter is 1, the second is 2, ...
所以你应该调用 setString(1,...)
和 setString(2,...)
而不是 setString(0,...)
和 setString(1,...)
我有一个像这样的简单 PreparedStatement 字符串:
INSERT INTO albums (name, date) VALUES (?, ?)
我用这个创建了 table:
CREATE TABLE "albums" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"date" TEXT NOT NULL
);
这是我能写的最简单的例子:
public static void main(String[] args)
Connection dbConnection;
String sqlAlbumQuery = "INSERT INTO albums (name, date) VALUES (?, ?)";
PreparedStatement statement;
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:plswork.sqlite");
}
catch (SQLException e) {
System.err.println("Failed to establish connection: " + e);
return;
}
try {
statement = dbConnection.prepareStatement(sqlAlbumQuery);
statement.setString(0, "some album"); //StatementTest.java:40
statement.setString(1, "2012"); //StatementTest.java:41
statement.executeUpdate();
}
catch (SQLException e) {
System.err.println("Failed to create album: " + e);
}
}
这是我收到的消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:130)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setString(JDBC3PreparedStatement.java:417)
at statementtest.StatementTest.main(StatementTest.java:40)
C:\Users\notangryatall\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\notangryatall\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:94: Java returned: 1
我将 Netbeans 11.3 与 JDK 1.8 和 sqlite-jdbc-3.30.1.
一起使用我尝试更改为 JDK 13,尝试使用旧的 sqlite-jdbc-3.23.1,尝试在 sqlAlbumQuery
的末尾添加分号,尝试使用 statement.executeQuery()
而不是 statement.executeUpdate()
并尝试 Google 但仍然找不到任何东西。
问题在这里:
statement.setString(0, "some album"); //StatementTest.java:40
准备语句索引从1开始(来自PreparedStatement接口):
@param parameterIndex the first parameter is 1, the second is 2, ...
the first parameter is 1, the second is 2, ...
所以你应该调用 setString(1,...)
和 setString(2,...)
而不是 setString(0,...)
和 setString(1,...)