This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JDBC and the fly likes Extracting a column value from a row Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Extracting a column value from a row" Watch "Extracting a column value from a row" New topic
Author

Extracting a column value from a row

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Folks...

I need to execute the following sql query and printout the resulting value to the stack trace. The query is as follows:



iduser is a primary key and therefore hoursused is a value from a column in that same row as 'iduser = p0248448'

This is what I did within the doPost method of the servlet:



I am getting this in my stack tace:



the vaue I am expecting is actually the number 6. I suspect it's cos I am printing the object itself, how do I actually print the value? Hope someone can advise. Thanks
Jijesh T Das
Greenhorn

Joined: Sep 27, 2007
Posts: 10
executeUpdate .getString(hoursused ) willl extract the required value.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2350

Welcome to JavaRanch, Jijesh.
Jijesh T Das wrote:executeUpdate .getString(hoursused ) willl extract the required value.
But don't forget to navigate to the first record in the resultset before calling ResultSet.get...()

regards, Jan


OCUP UML fundamental
ITIL foundation
Jijesh T Das
Greenhorn

Joined: Sep 27, 2007
Posts: 10
Thanks Jan
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Guys...

Thanks for the replies....I am just curious...shouldn't it be executeQuery instead of executeUpdate??. Anyway based on the advise given, I have managed to get it to work. For the benefit of those caught in a simillar predicament as me, I am posting the code below:



Thanks once again guys...
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2350

Varnam Aayiram wrote:..shouldn't it be executeQuery instead of executeUpdate...
True. executeUpdate has some additional functionality that you don't need in a select statement (see javadoc for more info, it's related to auto generated keys). It is a proper approach to use executeQuery for select statements.

and thank you for sharing the solution.

Jan
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
most welcome Jan
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Extracting a column value from a row
 
Similar Threads
PreparedStatement binding not working properly (Oracle Problem?)
SetNull in a PreparedStatement
Select query based on userinput
jdbc question
using Hibernate with Session Bean