0

I am trying to get the total count of a table rows using the below code.

            Connection con = getJdeConnection(userN, password, hostN, dbName);
            PreparedStatement p = con.prepareStatement("select count(*) from F0010");
            System.out.print("\n----- Connection Success ---------------\n\n");
            ResultSet rs = p.executeQuery();                    
            if(rs.next()){
                System.out.println("Query = select count(*) from F0010");
                System.out.println("count = "+rs.getInt(1));
            }else{
                System.out.println("zero records");
            }

The above code is running fine if am running the above code for my test table. Means it is giving the exact count. But I need to count number of rows in the above table F0010. This is a table in JD Edwards, contains 5203 records. The same sql when am running in JDE sql prompt it is giving count as 5203. But when am running this query from java code it is giving count as 0 (Zero).

My program is not throwing any exceptions.

Please help me. Thanks in advance.

Abdul
  • 1,130
  • 4
  • 29
  • 65
  • are you sure that you are connecting to the correct db? – Karthik Jul 09 '15 at 09:36
  • debug and check if you are getting the connection , you are calling - rs.next() in IF condition (in your case it is correct as you are expecting only one row ) - try using while (rs.next()) { ... } .. – Ashish Shetkar Jul 09 '15 at 09:45
  • Karthik - Yes am connecting to the correct db. Ashish - I am sure am getting connection. Otherwise I was unable to execute prepareStatement() . – Abdul Jul 09 '15 at 10:35
  • @Abdul qualify the table in the query, to be sure you are in the right schema `select count(*) from {owner}.F0010`. Additionally the else is misplaces; even if the table has no records you get one record back with zero. – Marmite Bomber Jul 09 '15 at 15:45
  • Thanks for your time Marmite. Thank you very much. Let me check with {owner}.F0010 . You are exactly correct that if i don't have records in table then I will get count=0. I know this. Let me check first part. Thank you. – Abdul Jul 15 '15 at 04:10

1 Answers1

0

Just to be sure you are running the query on the right schema change it into select count(*) from PRODDTA.F0010

where PRODDTA is the owner of your table.

Bruno_Condemi
  • 191
  • 2
  • 4