how to put a date in jdbc namedparameter - java

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.

Related

Apache Solr date field conversion error in java

I have a problem with a solr Date format. In a jar file the following row generates a conversion error:
**cannot cast 'java.util.ArrayList' to 'java.util.Date'**
Date date = (Date) list.get(0).getFieldValue(fieldName);
The line that generates the error is from mycore-oai-2019.11.jar and MCROAISolrSearcher.java class and as you know I cant change it.
The date column in solr is saved in the following format:
"modified":["2018-08-17T06:10:55Z"]
Debbuging the code in that row I see the following Date format :
I cant understand the error! I am trying for hours to figure it out but nothing. Is the Timezone the problem? Is the mapping of the field a problem? Please any advice/help would safe me!
Thank you in advance!
The field in Solr is a multivalued field - the code expects it to be single valued. A multi valued field returns a list (that's the arraylist you're seeing), while a single valued field returns the field directly (which would be the Date field).
Change your schema (remove multiValued="true" or explicitly set multiValued="false") for the field) and re-index.
By default, in schemaless mode, all fields are multivalued, so that's usually why this appears - you haven't explicitly configured your schema (which you should do in production use).

JPA StoredProcedureQuery get wrong values starting from parameters

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.

Retrieving MySQL time in java

I am running the following query to get time difference of two times
resultSet = statement.executeQuery("SELECT TIMEDIFF('14:03:55.256', '14:02:51.780') AS td");
MySQL gives time difference in this format
00:01:03.476
But both
resultSet.getTime("td"); and resultSet.getObject("td");
returns 00:01:03
According to the documentation getTime(String string) retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.
java.sql.Time corresponds to SQL TIME and contains information about hour, minutes, seconds and milliseconds.Then why am I getting 00:01:03 instead of 00:01:03:476?
What I basically want is to store 00:01:03.476 into a String. Is there any workaround or I am doing it wrong?
If you are verifying the result by printing it out note that java.sql.Time.toString() only returns a string in the format hh:mm:ss
You should really use rs.getTimestamp which will return a java.sql.Timestamp
Try to do this:
String s = new SimpleDateFormat("HH.mm.ss.SSS").format(t.getTime());
where t is your variable of type java.sql.Time.
Hope this will solve you problem.

lotus notes search by date with Java api

I'm trying to select records by date from a Lotus Notes database and have run into trouble with correctly formatting the date.
Here's the relevant code:
public void runNotes() {
Session s;
try {
s = NotesFactory.createSession((String)null, (String)null, "mypassword");
Database hkDB =
s.getDatabase("NBHDH001/YNM", "H\\DHH00001.nsf", false);
DocumentCollection docs = hkDB.search("[Date]>[2012/03/20]");
Date is a field in the record, and when I looked up records (with FTSearch), the date came back in the format above: [yyyy/mm/dd].
The parameter of the search is what I need here.
i.e. what should I put instead of "[Date]>[2012/03/20]"
I tried various constructions with Calendar and DateFormat, but it's not coming together...
Any suggestions?
You should get rid of the square brackets on the field name. The search method expects a Notes Formula, like what you'd put into a view selection formula:
"Date > [03/20/2012]"
It might also be required that dates are in mm/dd/yyyy format, though if you are in a non-US locale I'm not 100% sure.
You mentioned that you have been doing full text searches in the database, so it is definitely worth mentioning this... If the database actually has a full text index, then you may want to consider using the NotesDatabase.FTSearch() method instead of NotesDatabase.Search(). The FTSearch method will be considerably faster for a large database.
The syntax for FTSearch is different from the syntax for Search. You could use either "FIELD Date > 03/20/2012" or "[Date] > 03/20/2012".

How to send a Timestamp field to Oracle stored proc. from Java despite the DB config?

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.

Categories