當我在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;
}
向我們展示您的JDBC代碼。 – RustyTheBoyRobot
你得到什麼錯誤? – shanmugamgsn
沒有被選中的記錄。當我在日誌中打印查詢時,我只是複製粘貼wuery並在SQL Developer客戶端中運行它,我正在用Min Create Timestamp獲取記錄。爲了調試目的,我簡化了查詢,除了where子句= 12345之外沒有任何條件,我也從Java獲取記錄。但我想讓它與子查詢一起工作,因爲我的要求是查找具有min創建時間戳的記錄 – user549432