Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To get max value

 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wish to retreive the max value from the column.

I am using prepare statement. I have used the agregate function but how to assign the retrived value into a variable

Thank you in advance.

 
Jan Cumps
Bartender
Posts: 2608
14
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahi,

Can you show us what you already tried?

You can get the result bu calling ResultSet.get...(1);
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:You can get the result bu calling ResultSet.get...(1);

Right. Remember to call ResultSet.next() first. People sometimes think they don't need it if there is just one row. It will always return true for an aggregate function but still needs to be called.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:Hi Mahi,

Can you show us what you already tried?

You can get the result bu calling ResultSet.get...(1);


Thank you for the reply,

PreparedStatement stm=con.prepareStatement("select max(col_name) from table_name");

now how to assign the value etrived to a variable.

thank you in advance



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi,
No value has been retrieved yet. What method do you call on PreparedStatement to execute the query and get a ResultSet object? (Yes, I know. I'm trying to encourage you to look at the JavaDoc for PreparedStatement as that exercise will make the answer more memorable.)
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Mahi,
No value has been retrieved yet. What method do you call on PreparedStatement to execute the query and get a ResultSet object? (Yes, I know. I'm trying to encourage you to look at the JavaDoc for PreparedStatement as that exercise will make the answer more memorable.)


Thanks ,

After the prepare statement i should use rs=stm.executeQuery. Than will me max value be stored in rs.

Thank you in advance

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi,
Correct. After you call executeQuery(), you will have a ResultSet containing one row and one column. That's when the above advice on methods to call takes effect.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Mahi,
Correct. After you call executeQuery(), you will have a ResultSet containing one row and one column. That's when the above advice on methods to call takes effect.


It helped me. Thank you for your time.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic