Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to execute an scalar query ?

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
I need to retrive just one column of one rows of a table , so i need just one value .
for example my statement is :


now i want to know , what is best way to execute and retrive the value total_login.
should i create a ResultSet for this query or jdbc provide specefic querying for this condition.


thanks
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet is the only way.

And by the way, a common mistake of people getting just one row is to forget to call next() on the ResultSet, to advance to the first row....
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raminaa,
As Stu noted, you still need a ResultSet. This is a common pattern:

If you know a value will be returned, like when doing "select count(*)" you can use:


In your case, you don't know whether a record will be found, so you should use:
 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your helps.
I thought there is some way to get just one value out of statement.
i remember that there was such thing in .net .

but i looks like that in java land we must use a resultSet and bear its creation distruction fee :-)

Thanks again
 
Nguyen Van Huong
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to create a function to return a string value, that is

I also tested the function but it wasn't sucess
error:
Nov 28, 2011 12:40:47 AM generateEmpID
SEVERE: null
java.sql.SQLException: ORA-00911: invalid character

Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags next time. See how much better your post looks with them. And it immediately pointed me to the ; inside your query. Remove that and try again.
 
Nguyen Van Huong
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course,


Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raminaa niilian wrote:I thought there is some way to get just one value out of statement. i remember that there was such thing in .net .

but i looks like that in java land we must use a resultSet and bear its creation distruction fee :-)


The difference is in coding semantics, the same creation/destruction "fees" are incurred regardless of using Java or C#. If you find yourself using single-row result sets a lot, write your own SingleRowResultSet as a wrapper around ResultSet. Then your SingleRowResultSet wrapper can determine what to do when zero rows are returned, as Jeanne Boyarsky mentioned.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic