I am writing a Client Server Application now when the Client sends a command to the Server the Record in the SQL Database is updated and On Update a Timestamp is set on a field. But now I want to read all the Users in the SQL Database that where online in the last hour with the Timestamp.
So this is how far I am:
SELECT username FROM users WHERE lastonline...
lastonline is the field with the timestamp of the users last update. I now have no Idea how to check if he was online in the last hour.
I thank you for you help.
Select username FROM user WHERE lastoinline>"date now - 1 hour" AND lastoinline<="date now"
Date dеNow = new Date();
Date веBefore = tdNow; tdBefore.setHours(tdBefore.getHours()-1);
String query = "SELECT username FROM user WHERE lastonline>='"+(dtBefore.getTime/1000)+"' AND lastonline<='"+(dtNow.getTime/1000)+"'
"
Compare the current hour with the hour on the timestamp's record.
If you are using sql server:
SELECT DATEPART(hh, GETDATE()) --current hour
SELECT DATEOART(hh, ColumnStoredInDB) --stored hour
Spoiler: Simply take the ColumnStoredInDB from the current hour and if the difference is < 1 that user has been in the system for the last hour. If the difference >=1 you can skip that record
Related
I have problem with HQL where I am setting the query parameters. One of them is Date. When I debug the code there is Date with time entering the method. I set the parameter using setParameter(timestamp, new Timestamp(date.getTime())) or query.setTimestamp...etc etc I used many combinations...
When I use p6spy to examine the SQL comming from app to the DB there is only '29-Jan-21' or other date without time.
I am using hibernate 5.1.0 final and postgre DB. I'll be glad for any help.
Example:
Query query = getSessionFactory().getCurrentSession().createQuery("SELECT user FROM UserEntity cr WHERE user.userStatus.id = :statusId AND :timestamp >= user.valid_to");
This is how I tried to set the timestamp parameter:
query.setParameter("timestamp", new Timestamp(date.getTime()));
query.setParameter("timestamp", date, TimestampType.INSTANCE);
query.setTimestamp("timestamp", date);
query.setTimestamp("timestamp", new Timestamp(date.getTime()));
Problem is that the generated SQL replace timestamp by '29-Jan-21' or other date I choose but without time. The date parameter comes to the method from UI and it contains full date with time.
String sql1="SELECT MAX(date),epf_rte_emp,epf_rte_com,etf_rte FROM Fixed_Rates1";
I'm using this query to select the record with the maximum date value, but it gives the latest updated record. I want the record with the maximum date.This query gives the record with maximum date in the SQLiteManager, but doesn't gives the required output in netbeans. Could someone please help me in this?
If you want one record, then something like this shoudl work:
select fr.*
from fixed_rates1 fr
order by date desc
limit 1;
Note: I am guessing you are using MySQL, because your query would fail in most other databases. The method for limiting results depends on the database.
If you want all rows with the maximum date:
select fr.*
from fixed_rates1 fr
where fr.date = (select max(fr2.date) from fixed_rates1 fr2);
I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.
Here is Mysql query:
SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM
F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
Frankly, I don't have any idea about how to write above query in HQl.
Can anyone please help me.
Could you try this, I couldn't test, if you it has a error please inform me;
String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()";
query = session.createSQLQuery(hqlQuery);
I have a table , there are 4 fields: name, start date and time (Timestamp) ,end date and time (timpestamp) , car.
In Java/MySQL,
I need to compare database start date time and end start date time and compare with value given in textbox date time field .
Now problem is that we need to book person(driver) and car, if car and person
are not booked in given time (that is checked by database) , then we can booked it else not.
Please tell me a logic/query to do this.
If you have code please mention it.
Try this query,
SELECT name FROM table having '2014-11-28' between start_date and end_date;
You can use this query to check availability.
SELECT name FROM table WHERE start_date < '2014-03-13 11:42:28' AND end_date > '2014-03-13 11:42:28' limit 1
If you get any name, then you can not book new driver. Also look at mysql date and time functions.
I'm making a library project in Java that is supposed to record data about members, books, users and borrowings. When I create a new object of the class Borrowing the system automatically notes the IssueDate.
The problem happens when I try to make a record of the book being returned with and UPDATE SQL QUERY and record the ReturnDate.The query works but in DB in the corresponding field I always get 30/12/1899 as a ReturnDate.
The SQL query goes like this:
UPDATE Booking SET Returned = 1, ReturnDate = "+ sdf.format(getReturnDate()) +" WHERE BookingID =" + getBookingID()
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
setReturnDate(new Date());
I decided to format it to String and format dd/mm/yyyy because that is the format Access recognizes when I enter it manually and it worked with all other queries (like creating a new Booking and IssueDate) in the project and it enters the real date.
I have already tried using java.sql.Date instead, switching to the US format (mm/dd/yyyy) and using # (hash marks) at the beginning and the end of the date string and none of those worked.
Do you have any other ideas?
Consider using a PreparedStatement instead, see Using Prepared Statements for more details
Instead of setting the "text" of the query, use setDate of the PreparedStatement and let the JDBC driver work it out