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.
Related
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 ....;
First off, admittedly, I am no DBA...so my SQL-fu is weak.
I was recently working on a project that had a pretty hefty report that did 10 inner joins. When ran against Prod data (SQL Server 2005) using the SQL Studio Management client, the query wasn't a barn-burner, but it returned in just under 20sec. However, when ran through Spring, 31min.
So, we got our DBA ninja on it, and he pointed out that the query plan would be different because the JDBC method would use a prepared statement, passing in the variables as parameters, whereas in the client those were hard-coded. So, he re-worked the query.
The resulting query now sets some declare variables up top, then uses those to create a local temp table, then uses the local temp table as part of the ultimate report query. He said we should be able to send all this as part of the same query string (compound query????). It looks something like this (obfuscated to protect the innocent):
declare #startdate DateTime
declare #enddate DateTime
set #startdate = DATEADD (dd, 0, DATEDIFF (dd, 0, '2013-03-01 00:00:00.000'))
set #enddate = DATEADD (dd, 1, DATEDIFF (dd, 0, '2013-08-08 00:00:00.000'))
CREATE TABLE #LATEST_BLAH_ACTION
(
FK_Blah_Timestamp DATETIME,
FK_Blah_Id VARCHAR(10),
Blah_Other_Thing VARCHAR(10),
[Latest Updated Date/Time] DATETIME
)
INSERT INTO #LATEST_BLAH_ACTION
SELECT FK_Blah_Timestamp, FK_Blah_Id, Blah_Other_Thing,
MAX(Blah_Other_Timestamp) AS [Latest Updated Date/Time]
FROM BlahTable
WHERE Blah_Another_Thing = 'Some value' AND
Blah_Other_Timestamp BETWEEN #startdate and #enddate
GROUP BY FK_Blah_Timestamp, FK_Blah_Id, Blah_Other_Thing
SELECT
-- Bunch of fields
From LATEST_BLAH_ACTION
-- Bunch of crazy Inner Joins and such
However it's not working. If I run this out of SQL Management Studio, I get back results. If I run it out of the Java code using Spring's SimpleJdbcTemplate, no error but no results.
Is it possible to run a (compound???) query like this using Spring, and if so, how? Do I have to do them individually, but as part of a transaction? Maybe use something other than SimpleJdbcTemplate?
I'm trying to call an Oracle stored procedure in a Java EE web application (java) using Spring 'CallableStatementCreator'. One of the inputs for the stored procedure is a DATE.
My attributeValue is a java.util.date and it correctly holds both DD-MM-YYYY and HH:MM:SS.
When using the following code:
callableStmt.setTime(6, new java.sql.Time(attributeValue.getTime()));
The result is the column in the DB (the stored procedure ultimately writes in the DB) is set to 1970-01-01 and the correct HH:MM:SS I pass as input. This worked in a previous version of my application (where I used JDBC lib 10.x.x.x)
If I use
callableStmt.setDate(6, new java.sql.Date(attributeValue.getTime()));
The DD-MM-YYYY is set correctly but the hour is set to 00:00:00.
So, what's the correct way to call and pass the attribute to this stored procedure? Also, any debug tips?
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
Oracle JDBD lib: ojdbc6-11.2.0.3
Try using a Timestamp:
callableStmt.setTimestamp(6, new java.sql.Timestamp(attributeValue.getTime()));
Using
callableStmt.setTimestamp(6, new java.sql.Timestamp(attributeValue.getTime()));
Didn't work in my case where I have a stored procedure overloaded where the only difference between my 3 functions is this last parameter: VARCHAR2, DATE or NUMBER. When using this code, the execution of my stored procedure fails because Oracle can't find out which method to use. (probably because JDBC lib doesn't map the timestamp to DATE column, as explained here )
The only way it worked was with:
callableStmt.setObject(6, new java.sql.Timestamp(attributeValue.getTime()), OracleTypes.DATE);
which seems to force the mapping between my Timestamp to DATE column in Oracle stored procedure while keeping the DD-MM-YYYY and HH:MM:SS.
I have the following code:
conditions.add("mydate = str_to_date('"+date_from_user+"', '%Y-%m-%d')");
the above works fine but since I am taking input from the user and shoving it in my query I'm risking the security of the query. So I wanted to use named template so I changed the code to:
conditions.add("mydate = str_to_date(':mydate', '%Y-%m-%d')");
namedParams.put("mydate", date_from_user);
However, the above code doesn't work and produces the following error message:
<SQLWarning ignored: SQL state 'HY000', error code '1411', message [Incorrect datetime value: ':mydate' for function str_to_date]>
so it seems that namedparameter isn't picking up the value..
Have you tried removing the quotes in ':mydate' and change it as below,
conditions.add("mydate = str_to_date(:mydate, '%Y-%m-%d')");
namedParams.put("mydate", date_from_user);
First check your DB server date format. Then give input in same format. create your date object in the same Object or pass string in same format.
I'm making a request from a java webapp to an Oracle' stored procedure which happens to have a Timestamp IN parameter.
The way info travels is something like:
javaWebApp --} webservice client --} ws --} storedProcedure
And I send the Timestamp param as a formatted string from the webservice client to the ws.
In the testing environment, it works sending:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
input.setTimestampField(dateFormat.format(new Date()));
As you see, a formatted string is sent. But in the production environment, it raises an exception
ORA-01830: date format picture ends before converting entire input string.
It relates to the format not being the same, possibly due to differences in configuration from one DB to the other. I know the testing environment should be a replica of the production site, but it is not in my hands to set them properly. And I need to send the Timestamp-as-a-formatted-string field despite the way they setup the database. Any ideas? Thanks in advance.
**** EDIT ****: I've found the way to make it work properly despite the particular configuration. It is as simple as setting the call instruction in the web service with the appropiate Oracle instructions. I mean, the calling to the Oracle stored procedure went from
"call PACKAGE.MYPROCEDURE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
to
"call PACKAGE.MYPROCEDURE(?,?,?,?,?,?,TO_TIMESTAMP(?, 'DD-MM-YYYY HH24:MI:SS'),?,?,?,?,?,?,?,?,?,?,?)"
while the format I set in the procedure calling matches the format sent by the webapp using the SimpleDateFormat stated in the original question, slightly modified:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Thank you all for the help and the ideas.
The default NLS_DATE_FORMAT generally doesn't include the time and only a two-digit year. It is probably either DD-MM-YY or MM-DD-YY.
If the WS receives a string and the database stored procedure needs a timestamp, then the two of them will need to negotiate the format mask. Either the WS, when it connects to the database, should set an explicit date format, or the database should be able to accept a string and convert it using a hard-coded format.
Unless there is some particular negotiation you have defined in the WS, nothing the JavaWebApp or WebServiceClient will be able to influence the format that the database assumes the WS is using.
All that said, I'd have a look around any other code at your end and see if there's anything doing a similar translation. You may find something else using a specific format.
What does your query look like in the input prepared statement? That error indicates that Oracle doesn't like the date format you have passed in. Your test environment may have a different NLS_DATE_FORMAT set on the database or machine/driver being used.