![](/img/trans.png)
[英]java.sql.SQLException: Parameter index of 9 is out of range (1, 8)
[英]java.sql.SQLException: Parameter index out of range - whitespaces in String
我正在嘗試通過 ServletClass 中的 PreparedStatement 更新數據庫中的表。 它引發 java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1)。 我想,問題是汽車字符串包含更多單詞,所以它包含空格,但我不知道如何准確解決它。 我試圖刪除准備好的語句中第二個問號周圍的兩個撇號,但沒有幫助。 去掉引號后,還是出現如下錯誤:
java.sql.SQLException:無法使用 executeQuery() 發出數據操作語句
這是代碼的摘錄:
private void updateCarAvailability(int value, String car) throws Exception {
Connection conn = null;
PreparedStatement prst = null;
try {
conn = ds.getConnection();
String sql = "update cars set available=? where name='?'";
prst = conn.prepareStatement(sql);
prst.setInt(1, value);
prst.setString(2, car);
prst.executeQuery(sql);
}
解決方案是刪除'
where name='?'
,所以它應該看起來像update cars set available=? where name=?
update cars set available=? where name=?
,並且您還應該在使用更新命令后將executeQuery
更改為execute
。
刪除?
周圍的單引號
String sql = "update cars set available=? where name=?";
它們不是必需的,因為實際值不是作為 SQL 語句的一部分傳遞,而是作為綁定參數傳遞。 並且綁定參數不需要“SQL 格式”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.