aspose file tools*
The moose likes JDBC and the fly likes Retrieving result from a result set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retrieving result from a result set" Watch "Retrieving result from a result set" New topic
Author

Retrieving result from a result set

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

I have a piece of code


but i am unable to fetch the correct result from database . I am always getting the value 4 in count , however in my database the value for MAX(count_logins) is 2 . Can anyone explain how i can get the right value from my resultset
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
Hi,
Try checking the databases where you are running SQL manually and through JDBC are same.

Regds,
Amit
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

amit


my database gives me the correct result .
My database table has columns as :-
Sno,SessionId,Count_logins,usename

Can we use alias in the query and use the same alias for a column when using the result set .I mean in my case the alias is
log so can i use result set as:-
rs.getInt("log");
and it shoukld return me the value in the column named log
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
okay,
I meant to say
1) Check the database instance(IP and SID) that gives you count - 4 as result - this is the one which you are using in JDBC program
2) Check the database instance(IP and SID) that gives you count - 2 as result - I think you are checking this manually running the SQL

regds,
amit
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

ragi singh wrote:my database gives me the correct result

How are you checking the results of your query ? Which tool are you using ? How do you login ? Which user ? Which schema ? Please clarify all these.


[My Blog]
All roads lead to JavaRanch
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi Christophe

My table name is sessions:-
and the table structure is as follows:-
Sno Number(30), Session_ID Varchar2(4000),Count_logins Number(30),Usename Varchar2(30)

and I am using sql developer for querying the database . I have used oracle database . Each time i loop in rs.next() loop the values for count are abnormal also result set displays more rows than in the table itself . I cannot understand the abnormal behaviour of result set .
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You're not answering all my questions. I'm not asking for the table layout.
So, with Sql Developer, which user, which password, which schema are you login in to ?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
Hello,
Can you post the connection string that is being used in SQLDeveloper?

Regds,
amit
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi ,

I am sorry for being late in reply . I got my question solved , actually I changed my code as follows:-

My original code :-


Thinking that the code coming from database is ordered in the way the rows were inserted . However my data coming from the database was in some random order . Every time a user logged in all the rows were retrieved from the database . So if the third user logs in my result set was giving me three records . I wanted the last record inserted as it contained the field which holds the latest value of a count variable (number field) . So my res_count variable was holding any of the three values (whichever was retrieved last ) that is why my result was inconsistent . So I changed my code as:-

In this case my res_count always holds the last (greatest value of count ) which is what i want . So i am able to get consistent result and my doubt gets solved .

Thank you folks .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving result from a result set