2011-11-04 86 views
0

當我在SQL Developer中運行它時,此查詢正在返回帶有人爲Pers_ID的最小創建時間戳的記錄,並且同一查詢未從Java JDBC連接返回任何值。從Oracle中查詢返回值,並從Java運行時沒有記錄

你能幫忙嗎?

select PERS_ID,CODE,BEG_DTE 
from PRD_HIST H 
where PERS_ID='12345' 
and CODE='ABC' 
and CRTE_TSTP=(
    select MIN(CRTE_TSTP) 
    from PRD_HIST S 
    where H.PERS_ID=S.PERS_ID 
    and PERS_ID='12345' 
    and EFCT_END_DTE is null 
) 

的Java代碼與Java程序

public static List<String[]> getPersonwithMinCreateTSTP(final String PERS_ID,final String Category,final Connection connection){ 
    final List<String[]> personRecords = new ArrayList<String[]>(); 
    ResultSet resultSet = null; 
    Statement statement = null; 
    String PersID=null; 
    String ReportCode=null; 
    String effBegDate=null; 

    try{ 
    statement = connection.createStatement(); 
    final String query="select PERS_ID,CODE,EFCT_BEG_DTE from PRD_HIST H where PERS_ID='"+PERS_ID+"'and CODE='"+Category+"'and CRTE_TSTP=(select MIN(CRTE_TSTP) from PRD_HIST S where H.PERS_ID=S.PERS_ID and PERS_ID='"+PERS_ID+"' and EFCT_END_DTE is null)"; 

if (!statement.execute(query)) { 
      //print error 
     } 
     resultSet = statement.getResultSet(); 
    while (resultSet.next()) { 
    PersID=resultSet.getString("PERS_ID"); 
    ReportCode=resultSet.getString("CODE"); 
    effBegDate=resultSet.getString("EFCT_BEG_DTE"); 
    final String[] personDetails={PersID,ReportCode,effBegDate}; 
      personRecords.add(personDetails); 
     } 


    } catch (SQLException sqle) { 
     CTLoggerUtil.logError(sqle.getMessage()); 
    }finally{ // Finally is added to close the connection and resultset 
     try { 
      if (resultSet!=null) { 
       resultSet.close(); 
      }if (statement!=null) { 
       statement.close(); 
      } 
     } catch (SQLException e) { 
        //print error 
     } 
    } 
     return personRecords; 
} 
+0

向我們展示您的JDBC代碼。 – RustyTheBoyRobot

+0

你得到什麼錯誤? – shanmugamgsn

+0

沒有被選中的記錄。當我在日誌中打印查詢時,我只是複製粘貼wuery並在SQL Developer客戶端中運行它,我正在用Min Create Timestamp獲取記錄。爲了調試目的,我簡化了查詢,除了where子句= 12345之外沒有任何條件,我也從Java獲取記錄。但我想讓它與子查詢一起工作,因爲我的要求是查找具有min創建時間戳的記錄 – user549432

回答

1

打印出你的SQL SELECT語句並粘貼到SQL * Plus,看看發生了什麼。很可能你沒有把你的變量設置爲你認爲的那樣。實際上,當你打印出SELECT語句而不運行它時,你很可能會看到錯誤 - 當需要上限時,小寫值等。

如果仍然看不到它,請發佈實際查詢從你的java代碼在這裏。

0

我來到這裏有類似的問題 - 只是認爲我會發布我的解決方案爲其他人 - 我沒有運行「COMMIT」後,我做了插入(通過sqlplus) - 哈哈!

0

數據庫表中有記錄,但JDBC客戶端無法檢索記錄。 表示JDBC客戶端沒有選擇權限。請在命令行上運行以下查詢:

grant all on emp to hr; 
相關問題