The DB persist java.util.date and i need a query to get all the data between 2 Dates with only using java.util.date.
I use the JPA 2.1
String sql = "SELECT * FROM table WHERE created > '"+date1+"' AND created < '"+date2+"'";
This query provides always a List with 0 elements
The problem is that your dates are being added to the SQL query as string values. The database is getting something like:
String sql = "SELECT * FROM table WHERE created > 'Wed May 25 14:33:18 SAST 2016' and created < 'Wed May 25 14:33:18 SAST 2016'";
The database is seeing it as a string and that explains why it isn't working.
The single best way to resolve it is use bind variables::
String sql = ""SELECT * FROM table WHERE created > ? AND created < ?"
//Create the query object using that SQL string, then set values
sqlQuery.setParameter(1, date1);
sqlQuery.setParameter(2, date2);
Then execute the prepared statement.
Related
In my application i have a java Timestamp dateCreated, which is inserted to a mysql Timestamp colum. Inserting is no problem, i use a prepared statement and statement.setTimestamp(dateCreated).
Now i need to select a row with the dateCreated as unique identifier. my method gets another java Timestamp object.how does the SQL query work in this case? i havent figured out how to compare the java timestamp to the mysql one.
SELECT * FROM table WHERE timestamp_column = ???
Thanks!
In Java you will use a similar setTimestamp method as you did with the insert.
Timestamp t = ???;
String sql = "SELECT * FROM table WHERE timestamp_column = ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setTimestamp(t);
etc.
I've created a program using Java that connects to a database and allows the user to submit a record. This record includes a unique reference number and started date (Variable: StartedDate).
I want to enable the user to search for the amount of cases submitted between two dates (the first day of the month and todays date).
So far I have the SQL query
select * from cases where StartDate>'***' and Date<'****'
In Java, it would be
select * from cases where StartedDate>'1stMONTHDATE' and Date<'TODAYSDATE'
My first question is - Within the database, there isn't a field called "Date" as this would have to change on a daily basis. How would I reference this to be the automatically generated date from Java?
Also, how would I implement the Count() method to return an int of the number of records returned?
SELECT * FROM cases WHERE StartedDate BETWEEN :startDate AND :endDate
In JPQL:
public List<Cases> findAllEvents(Date startDate, Date endDate) {
List<Cases> allCases = entityManager.createQuery("SELECT * FROM cases WHERE StartedDate BETWEEN :startDate AND :endDate")
.setParameter("startDate", startDate, TemporalType.DATE)
.setParameter("endDate", endDate, TemporalType.DATE)
.getResultList();
return allCases ;
}
Well i suppose by "Date" in the query you mean end date.
if that is so you can calculate the end date with either of the two following methods
java using the Calender class and pass the end date as parameter to the query
In the sql by modifying the query like
for ex- here the end date is one month from start date.This is DB specific.This will work in Postgres. Look for the correct syntax according to the DB you use to get the end date
select * from cases where StartedDate>'1stMONTHDATE' and ('StartedDate' + INTERVAL '1 MONTH')<'TODAYSDATE'.
Regarding the count()-A simple SELECT Count(*) will give you the number of rows returned by thw query
If you are using MSSQL and your date values are as String then you can use following query:
SELECT c FROM cases c where CONVERT(VARCHAR(10),StartDate,121) between :startDate AND :endDate
I am using this query in my application with mssql server
I am using jpa 3.o with Hibernate. I have one named query:
SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate>= CURRENT_DATE
WPSTransaction is my entity class and createdDate is one of the columns in my class.
It's working fine in the Mysql Database. However, I'm moving to SQL Server 2012 and SQL server doesn't seem to compile the CURRENT_DATE value. I've tried GETNOW() and NOW() methods as well as current_date() method and CURRENT_TIMESTAMP without any luck.
MS SQL Server fails to conform to the standard here - it does not provide a current_date keyword.
You must instead use the SQL-server specific GETDATE() function, as the document linked shows.
Because current_date is a keyword in the spec, you can't just create a user-defined current_date() function in MS SQL and expect it to work the same. So unfortunately you're stuck with database-specific code here.
The function to return the current date in MS SQL is GETDATE(), so your query should read
SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate >= GETDATE()
How about:
Query q = entityManager.createQuery("SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate>= :d");
q.setParam("d", new Date());
No database specific code needed.
I have successfully insert data with LOAD DATA INFILE. After that when I am using the query:
SELECT * FROM tempupload
WHERE columnName NOT IN (SELECT columnName FROM othertable)
Not giving me the desirable result. But when I convert columnName datatype to double (and after again to varchar) giving me desirable results.
Please guide me as I have to use these queries from my Java EE Application.
If you have any NULLs in your othertable you may have some issues with a NOT IN (see this). To improve your query you could try:
SELECT * FROM tempupload
WHERE EXISTS (SELECT 1
FROM othertable
WHERE othertable.columnName = tempupload.columnName)
I am trying to write an hql query which gives me the number of hours between two timestamp.
So, far i am unable to do this. I have used hql hour function but that does not work if the
timestamp corresponds to different date. Please provide any input.
My hql query is
select count(*) from com.xxx.Request as request where request.id = :id and hour(current_timestamp - request.lastEventDate) > :delay
well, you can use:
days(first_timestamp) * 24 + hours(first_timestamp)
- days(second_timestamp) * 24 - hours(second_timestamp);