1

This query is returning the record with Min Create time Stamp for the Person Pers_ID when I run it in SQL Developer and the same query is not returning any value from Java JDBC connection.

Can you please help?

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 Code

 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;
}
3
  • There are no record that is getting selected . When I print the query in the logs and I just copy paste the wuery and run it in SQL Developer client I am getting a record with Min Create Timestamp. For debugging purpose I simplified the query and didnt have any condition except a where clause=12345 and I get the record from Java as well. But I want to have it work with the sub query as my requirement is to find the record that has min create time stamp for the person Commented Nov 4, 2011 at 19:16
  • Are you sure you are connecting to the same database with the same user from within your Java application? Commented Nov 4, 2011 at 21:24
  • There was an issue with the person id 12345 - The moment I changed the ID its atrted working - Thanks a lot guys and sorry for posting this - Really appreciate your help Commented Nov 4, 2011 at 21:40

3 Answers 3

1

Print out your SQL SELECT statement from your java program and paste it into SQL*Plus and see what is happening. It's likely you're not getting your variables set to what you think you are. In fact, you're likely to see the error when you print out the SELECT statement without even running it - lower case values when upper is needed, etc.

If you still can't see it, post the actual query from your java code here.

Sign up to request clarification or add additional context in comments.

Comments

0

I came here with similar problem - just thought I'd post my solution for others following - I hadn't run "COMMIT" after the inserts I'd made (via sqlplus) - doh!

Comments

0

The database table has records but the JDBC client can't retrieve the records. Means the JDBC client doesn't have the select privileges. Please run the below query on command line:

grant  all on emp to hr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.