【问题标题】:ORA-00933: SQL command not properly ended in PreparedStatementORA-00933: SQL 命令未在 PreparedStatement 中正确结束
【发布时间】:2016-02-04 20:21:03
【问题描述】:

我有一个运行良好的 oracle SQL 语句:

select user_id,email from app_users where user_id in (select user_id from app_users_groups where group_id = ?);

我运行它并得到了我期望的结果,但是我试图通过以下方式在 Java 中运行它:

    try (Connection conn = dataSource.getConnection()) {
        try (PreparedStatement ps = conn.prepareStatement("select user_id,email from app_users where user_id in (select user_id from app_users_groups where group_id = ?);")) {
            ps.setInt(1, groupId);
            try (ResultSet rs = ps.executeQuery()) {
                while(rs.next()) {
                }
            }
         }
     } catch (SQLException e) { }

但是我在ps.executeQuery 失败并出现 SQLException ORA-00933: SQL 命令未正确结束。

我不太确定问题是什么,尽管我确定这是我缺少的一些简单的东西。

谢谢

【问题讨论】:

  • 去掉查询末尾的分号
  • 你应该发布堆栈跟踪
  • Dmitry 是对的:您需要删除字符串中的;。但是您还应该在子查询中完全限定列名user_id 以避免名称冲突。

标签: java sql oracle


【解决方案1】:

正如 Dmitry.P 和 a_horse_with_no_name 所说。

我只需要删除分号 - 也是按照建议限定的列名。

conn.prepareStatement("select u.user_id,u.email from app_users u where u.user_id in (select g.user_id from app_users_groups_xref g where g.group_id = ?)")

我的头脑很简单,谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-09
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多