I'm trying to list the dates registered in a table with SQL Server, but my problem is all the dates I'm extracting differs of 2 days with the dates in the table.
For example, I got 2012-12-25 in my database and when I retrieve it and cast it to a Java.util.Date, it becames 2012-12-23...
I've got processes on dates in another table which are working fine.
I'm using SQL Server 2008, Hibernate 3 and Spring 3.
Edit:
The column data type for the table is date, I'm retrieving it using hibernate so here is my hibernate query call:
public List<Holiday> retrieveAllHolidays() {
return (List<Holiday>) sessionFactory.getCurrentSession().createQuery("from Holiday")
.list();
}
The holiday object got two attributes: a String and a Date (this one is incorrect after retrieving from database).
The problem was linked with the JRE 7 support of the JDBC Driver (a problem with the getDate() function).
See this post for more informations: Microsoft JDBC driver team blog
Thanks again Martin smith for pointing to that other issue!
Related
I got a problem with JPA StoredProcedureQuery, the problem consist I'm getting different values unexpected. In MySQL workbench I execute the stored procedure called sp_GET_Result that it is waiting two parameters called startDate and endDate.
For instance:
And the results are like:
So far it's ok, but the problem is when I call it from Java JPA.
This is my java routine
I'm getting this results:
[2017-01-31, 2017-02-01, 2017-02-02, 2017-02-03, 2017-02-04, 2017-02-05, 2017-02-06, 2017-02-07, 2017-02-08, 2017-02-09, 2017-02-10, 2017-02-11, 2017-02-12, 2017-02-13, 2017-02-14, 2017-02-15, 2017-02-16, 2017-02-17, 2017-02-18, 2017-02-19, 2017-02-20, 2017-02-21, 2017-02-22, 2017-02-23, 2017-02-24, 2017-02-25, 2017-02-26, 2017-02-27]
I think the problem not is with the store procedure if not with the parameters that java is sending.
Because I did a try out printing the parameters that Java is sending and MySQL is receiving and the results are like:
starDate , endDate
[2017-01-31, 2017-02-27] but Why?? because I'm sending from 2017-02-01 to 2017-02-28.
The problem not is the store procedure, the really problem is the variation in the results.
Most probably your JDBC driver converts your input dates into the UTC format, which is used by database. Or via versa, your response dates are converted into your local timezone. Check the JDBC driver setting: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html.
I want to store and retrieve data (a single column table for each date) date wise in MySQL database through java. Any suggestions on how to do it?
Any transaction with database through java is possible using JDBC library. JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connect to the database.
Here is an overview of the basic steps involved:
Registering the driver class
Class.forName("com.mysql.jdbc.Driver").newInstance();
Creating connection
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=username&password=password");
Creating statement
Write your MySQL query for storing or retrieving data from database date wise
Executing queries
Based on the query, you may get some records returned as a result or the count of rows affected
Closing connection
For more details, please refer following links:
link1
link2
I have a Date type column in one of our tables and when I query the database I get the date from the resultset like:
final String createdDate = resultSet.getString("received_date");
If I run this on OC4J container on Java 5 I get
2014-12-26 05:57:31.0
while on Tomcat with Java 8 I get
2014-12-26 05:57:31
The JDBC driver are of different versions - do you think I should blame the driver for this? Is it a good direction regarding the investigation? Or is it more about Java version difference?
Thanks
We are using ojdbc14_10.1.0.2.jar with a Java/J2EE application (that use directly JDBC) and JDK5, but when we tried to migrate into ojdbc5-11.2.0.3.jar we encountered an issue related to some sql request (jdbc) that doesn't work anymore.
The pseudo SQL request is :
select *
from quotas q
where q.datdeb<='2013-09-05' and q.datfin>='2013-09-05'
and q.datdeb is not null and q.datfin is not null order by ....;
The NLS parameters for date is :
DD/MM/RR
Which is not compatible with the date format giving as parameter in the request.
Everything worked fine when we were using ojdbc14; apparently it does an implicite conversion for the date.
For information, The oracle Database is 11g Release 11.2.0.3.0 - 64bit
Best regards.
I believe you just need to use the to_date function with an appropriate date mask to get around the problem.
select *
from quotas q
where q.datdeb<=to_date('2013-09-05','yyyy-mm-dd') and q.datfin>=to_date('2013-09-05', 'yyyy-mm-dd')
and q.datdeb is not null and q.datfin is not null order by ....;
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.