This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to execute an scalar query ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to execute an scalar query ?" Watch "How to execute an scalar query ?" New topic
Author

How to execute an scalar query ?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
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

Joined: Dec 15, 2005
Posts: 333
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
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

Joined: Nov 28, 2011
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nguyen Van Huong
Greenhorn

Joined: Nov 28, 2011
Posts: 5
Of course,


Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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.


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
 
wood burning stoves
 
subject: How to execute an scalar query ?
 
Similar Threads
How to Retrive the hidden field value
retrieve a single value inside string
questions about statement types and performace .
Data Retrieval Time
how can i get a value in java script function from database