java.sql.SQLException:参数索引超出范围 - 字符串中的空格

我正在尝试通过 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);

    }


婷婷同学_
浏览 194回答 2
2回答

慕田峪4524236

解决方案是删除'on where name='?',因此它应该看起来像update cars set available=? where name=?,并且一旦您使用更新命令,您也应该更改executeQueryfor execute。

汪汪一只猫

去掉周围的单引号 ?String sql = "update cars set available=? where name=?";它们不是必需的,因为实际值不是作为 SQL 语句的一部分传递,而是作为绑定参数传递。并且绑定参数不需要“SQL 格式”。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java