1

当我在 SQL Developer 中运行此查询时,此查询将返回带有 Person Pers_ID 的 Min Create time Stamp 的记录,并且相同的查询未从 Java JDBC 连接返回任何值。

你能帮忙吗?

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 代码

 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;
}
4

3 回答 3

1

从您的 java 程序中打印出您的 SQL SELECT 语句并将其粘贴到 SQL*Plus 中,看看发生了什么。您可能没有将变量设置为您认为的样子。事实上,当您打印 SELECT 语句而不运行它时,您可能会看到错误 - 需要大写时的小写值等。

如果您仍然看不到它,请在此处发布您的 java 代码中的实际查询。

于 2011-11-04T21:12:40.637 回答
0

我带着类似的问题来到这里 - 只是想我会为其他人发布我的解决方案 - 我在插入后没有运行“COMMIT”(通过 sqlplus) - 哦!

于 2014-10-11T12:38:50.873 回答
0

数据库表有记录,但 JDBC 客户端无法检索记录。表示 JDBC 客户端没有选择权限。请在命令行上运行以下查询:

grant  all on emp to hr;
于 2016-11-15T16:39:38.580 回答