【问题标题】:Why I get "java.sql.SQLException: Invalid column index "?为什么我得到“java.sql.SQLException:无效的列索引”?
【发布时间】:2016-11-23 11:09:37
【问题描述】:

我尝试将数字传递给 PrepareStmt,但收到此错误。我无法理解我的问题。

查询:

private static final String SQL_FIND_ALL_CALENDARS = "SELECT * FROM calendar WHERE idCall > '?';";

功能:

private List<Calendar> findAll(Connection con) throws SQLException {
        List<Calendar> calendar = new ArrayList<Calendar>();
        PreparedStatement prsmt = null;
        ResultSet rs = null;
        try {
            prsmt = con.prepareStatement(SQL_FIND_ALL_CALENDARS);
            prsmt.setInt(1, TestOracleJDBC.idCall);
            rs = prsmt.executeQuery();
            while (rs.next()) {
                Calendar calendar2 = extractCalendar(rs);
                calendar.add(calendar2);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            rs.close();
            prsmt.close();
        }
        return calendar;
    }

在其他类中我的参数:

public static int idCall = 1;

【问题讨论】:

  • 三件事:(a)你的extractCalendar方法是做什么的?请编辑问题以包含它,(b)不要在查询中使用分号,(c)如果没有帮助,请解释为什么不这样做。说明您希望代码做什么以及它当前在做什么。

标签: oracle select jdbc where


【解决方案1】:

单引号 (') 表示 SQL 中的字符串文字。使用绑定变量时,您不应该用引号将 ? 括起来 - 这会将其更改为带有问号的字符串文字,而不是绑定值的占位符。

只要删除它就可以了:

private static final String SQL_FIND_ALL_CALENDARS =
    "SELECT * FROM calendar WHERE idCall > ?";
    // Here -------------------------------^

【讨论】:

  • 很遗憾,它对我没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 2015-03-23
相关资源
最近更新 更多