0

I will try to explain what I want to do. I have a table in which I store the time that a user enters to work and update this row when the user leaves his work. The fields are timeIn and timeOut. Before I store the timeOut, I want to display how many hours the user has worked so far. So I think I have to retrieve the timeIn and calculate the difference with the actual hour of the system. But I don't know how to retrieve only one field of a table. I reckon I have to create an object time(for example) and get the timeIn along with other parameters and then calculate the difference. But I don't know whether I'm right and how to do that.

Cheers

2
  • 2
    You don't know how to execute a SQL query? You'd better learn SQL before using any database. Commented Feb 17, 2012 at 13:46
  • No relevance for tag servlets Commented Feb 17, 2012 at 14:11

1 Answer 1

1

See here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff

You may be able to get the hours worked straight from MySQL thusly:

SELECT TIMEDIFF(NOW(), timeIn) AS hoursWorked
FROM yerTable
WHERE personIdOrWhatever...

You didn't tell us what type of field timeIn is. It should be datetime.

Sign up to request clarification or add additional context in comments.

9 Comments

timeIn and timeOut is timestamp. I din't understand AS hoursworked. What AS does mean?
And another question is: Will I have to storage the result in a resultSet?
AS is a way to name a field. It helps to explain what your code is doing. As for the ResultSet, that could take some explaining. There's a tutorial here: docs.oracle.com/javase/tutorial/jdbc/basics that should help you get started.
So if I do a select like this I still have to storage in the resultset object, right? But how I can get the result as String. I'm using prepared statement. Hence, I have to indicate the name of the column that I'm retrieving the result, getString("hoursworked")?
and another thing is, I've tried this query and it is returning null; however, when I replace now() to time_out it works. Do you know why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.