簡體   English   中英

查詢在 SQL 開發人員中運行良好,但給出錯誤消息 = ORA-00905:JDBC 中缺少關鍵字

[英]Query runs fine in SQL Developer but gives Error Msg = ORA-00905: missing keyword in JDBC

我正在嘗試從代碼中運行這個 SQL 准備好的語句。

select
    COUNT(*)
from table1 ed, table2 e
where ed.id = e.id
  and e.status_cd = ?
  and ed.active_ind = 1
  and {in}
  and systimestamp < FROM_TZ(cast(ed.end_effective_dt_tm as TIMESTAMP), ?)
  and FROM_TZ(cast(? as TIMESTAMP), ?) between TIMESTAMP ? and TIMESTAMP ?

讀取此內容並針對 Oracle 數據庫執行的 JDBC 代碼讀取如下內容:

int parameterIndex = 0;
      stmt.setDouble(
          ++parameterIndex, doubleValue);
stmt.setInClause(
          ++parameterIndex, inClauseColumns.toArray(new InClauseColumn[inClauseColumns.size()]));
      stmt.setString(
          ++parameterIndex, tz_Id); /* Setting time zone for casting ed.end_effective_dt_tm */
      stmt.setString(++parameterIndex, timeFilterColumn);
      stmt.setString(++parameterIndex, tz_Id); // timeFilter
      stmt.setTimestamp(
          ++parameterIndex, new Timestamp(startTime), calculationTimeZone); // startTime
      stmt.setTimestamp(++parameterIndex, new Timestamp(endTime), calculationTimeZone); // endTime
      ResultSet rs = null;
      try {
        while (stmt.hasNext()) {
          rs = stmt.next();
   // do stuff

這會在 JDBC 中產生以下錯誤:

Error Msg = ORA-00905: missing keyword

但是,當從 SQL 開發人員運行時,相同的查詢會從數據庫返回預期的行。

示例查詢來自 SQL 開發人員:

select
    COUNT(*)
from table1 ed, table2 e
where ed.id = e.id
  and e.status_cd = 854 /*Prameter 1*/
  and ed.active_ind = 1
  and ed.facility_cd in (1.7241194E7) /*in clause parameter 2 */
  and
  systimestamp < FROM_TZ(cast(ed.end_effective_dt_tm as TIMESTAMP), 'America/Chicago' /*parameter 3 */)
  and
  FROM_TZ(cast(e.updt_dt_tm /*parameter 4 */ as TIMESTAMP), 'America/Chicago') /*parameter 5 */
  between
  TIMESTAMP '2021-06-30 02:23:20.0' /*parameter 6 */
  and TIMESTAMP '2021-11-10 18:09:24.774' /*parameter 7 */

有人可以就如何使用 JDBC 提供一些建議嗎? 我似乎無法弄清楚這里的問題。 謝謝。

查詢問題:

  • 不正確的 in 子句and {in}只是ed.facility_cd in (? )並為每個數組元素設置參數
  • 如果將特定java.sql.Timestamp類型設置為 JDBC 語句,則不需要between TIMESTAMP? and TIMESTAMP? between TIMESTAMP? and TIMESTAMP? 只是between? and? between? and?

數據庫結構:

CREATE TABLE TABLE1 (
 ID NUMBER,
 active_ind NUMBER,
 end_effective_dt_tm TIMESTAMP,
 facility_cd FLOAT
);

CREATE TABLE TABLE2 (
 ID NUMBER,
 status_cd FLOAT,
 updt_dt_tm TIMESTAMP
);

工作 JDBC 語句的示例:

    public Long execute(Connection connection) throws SQLException {
        long count = 0L;

        double doubleValue = 854D;
        Double[] inClauseValues = new Double[]{1.7241194E7, 1.7241194E8};
        String tz_Id = "America/Chicago";
        Timestamp startTime = Timestamp.valueOf("2021-06-30 02:23:20.0");
        Timestamp endTime = Timestamp.valueOf("2021-11-10 18:09:24.774");
        String timeFilterColumn = "e.updt_dt_tm";
        String inClauseColumn = "ed.facility_cd";

        String sqlQuery = " select COUNT(*) " +
                " from table1 ed, table2 e " +
                "  where ed.id = e.id " +
                "  and e.status_cd = ? " +
                "  and ed.active_ind = 1 " +
                "  and ? in ( " + Arrays.stream(inClauseValues).map(v -> "?").collect(Collectors.joining(", ")) + " ) " +
                "  and systimestamp < FROM_TZ(cast(ed.end_effective_dt_tm as TIMESTAMP), ? ) " +
                "  and FROM_TZ(cast( ? as TIMESTAMP), ? ) between ? and ? ";

        try (PreparedStatement stmt = connection.prepareStatement(sqlQuery)) {
            int parameterIndex = 0;

            stmt.setDouble(++parameterIndex, doubleValue); // Setting e.status_cd
            stmt.setString(++parameterIndex, inClauseColumn); //Set dynamic column for in cluase
            for (Double value : inClauseValues) { //Setting ed.facility_cd in
                stmt.setDouble(++parameterIndex, value);
            }
            stmt.setString(++parameterIndex, tz_Id); /* Setting time zone for casting ed.end_effective_dt_tm */
            stmt.setString(++parameterIndex, timeFilterColumn); //Setting timeFilterColumn e.updt_dt_tm
            stmt.setString(++parameterIndex, tz_Id); /* Setting time zone for casting e.updt_dt_tm  */
            stmt.setTimestamp(++parameterIndex, startTime); // startTime
            stmt.setTimestamp(++parameterIndex, endTime); // endTime

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    count = rs.getLong(1);
                }
            }
        }
        return count;
    }

更新
另一種解決方案,您可以使用動態列預構建查詢字符串:

    public Long execute(Connection connection) throws SQLException {
        long count = 0L;

        double doubleValue = 854D;
        Double[] inClauseValues = new Double[]{1.7241194E7, 1.7241194E8};
        String tz_Id = "America/Chicago";
        Timestamp startTime = Timestamp.valueOf("2021-06-30 02:23:20.0");
        Timestamp endTime = Timestamp.valueOf("2021-11-10 18:09:24.774");
        String timeFilterColumn = "e.updt_dt_tm";
        String inClauseColumn = "ed.facility_cd";

        String sqlQuery = " select COUNT(*) " +
                " from table1 ed, table2 e " +
                "  where ed.id = e.id " +
                "  and e.status_cd = ? " +
                "  and ed.active_ind = 1 " +
                "  and " + inClauseColumn + " in ( " + Arrays.stream(inClauseValues).map(v -> "?").collect(Collectors.joining(", ")) + " ) " +
                "  and systimestamp < FROM_TZ(cast(ed.end_effective_dt_tm as TIMESTAMP), ? ) " +
                "  and FROM_TZ(cast( " + timeFilterColumn + " as TIMESTAMP), ? ) between ? and ? ";

        try (PreparedStatement stmt = connection.prepareStatement(sqlQuery)) {
            int parameterIndex = 0;

            stmt.setDouble(++parameterIndex, doubleValue); // Setting e.status_cd
            for (Double value : inClauseValues) { //Setting ed.facility_cd in
                stmt.setDouble(++parameterIndex, value);
            }
            stmt.setString(++parameterIndex, tz_Id); /* Setting time zone for casting ed.end_effective_dt_tm */
            stmt.setString(++parameterIndex, tz_Id); /* Setting time zone for casting e.updt_dt_tm  */
            stmt.setTimestamp(++parameterIndex, startTime); // startTime
            stmt.setTimestamp(++parameterIndex, endTime); // endTime

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    count = rs.getLong(1);
                }
            }
        }
        return count;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM