-1

I am converting my date param from "yyyy-MM-dd" to "yyyy/MM/dd" using the below code

String dateParam = new SimpleDateFormat("yyyy/MM/dd").format(inpDate);

where inpDate = 2021-05-05 (date type) When I came across a line of resultSet, it traverse next directly to catch statement, before checking the if condition and I facing an exception

Literal does not match format string

I tried all possibilities but unable to get rid of this exception.

public boolean dateInfo (Date inpDate, Connection conn)
{
    StringBuilder queryStr = new StringBuilder();
    ResultSet resultSet = null;
    PreparedStatement ps = null;
    String dateParam = new SimpleDateFormat("yyyy/MM/dd").format(inpDate);
    queryStr.append("SELECT * FROM TABLE WHERE DB_DATE>= ?");
    try{
        ps = conn.prepareStatement(queryString.toString());
        ps.setString(1,dateParam);
        resultSet = ps.executeQuery();
        if(resultSet.next())
        {
            return true;
        }
    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    finally {
        DbAccessUtility.closeStatements(ps,resultSet);
    }
    return false;
}
Abra
  • 19,142
  • 7
  • 29
  • 41
George.G
  • 1
  • 5
  • 2
    Although you may be using an Oracle or MySQL database (they are different RDBMS), the issue and the code in the question does not appear to be directly relating to either and this is entirely a Java issue. I am removing the unnecessary tags; if you feel that they were needed then edit them back in (but make sure you explain why they particularly relate to the problem). – MT0 May 05 '21 at 16:01
  • your method has `inpDate` that is unused, instead you are formatting `myDate` which is not declared in this scope. I do not understand why you are parsing the date into a string at all? Maybe this will help: https://stackoverflow.com/q/18614836/1690217 – Chris Schaller May 06 '21 at 01:12
  • Does this answer your question? [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) – Ole V.V. May 06 '21 at 04:10
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead since JDBC 4.2 use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 06 '21 at 04:12
  • Did you try pasting your error message into your search engine? I get a long list of hits. – Ole V.V. May 06 '21 at 04:14
  • [Edit] your question and post the **entire** error message **and** stack trace. – Abra May 06 '21 at 05:12

2 Answers2

1

You do not provide enough detail for a diagnosis or cure.

But I can tell you to:

  • Stop using terrible date-time classes that were supplanted years ago by the modern java.time classes defined by JSR 310.
  • Exchange smart objects with your database rather than dumb strings.

You said:

I am converting my date param from "yyyy-MM-dd" to "yyyy/MM/dd"

Don’t.

Parse your input string as a LocalDate object.

LocalDate ld = LocalDate.parse( "2021-05-05" ) ;

Use a prepared statement with ? placeholders. So your SQL looks so,etching like this:

SELECT * FROM some_table_ WHERE some_column_ < ? ;

Using a JDBC driver compliant with JDBC 4.2 or later, exchange the LocalDate object.

myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

Notice how no strings were involved as database values, just java.time objects.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you sir, i have provided my full code, actually i am getting date as Date format, and convert it from -(hypen) into (/) slash, and stored as string , and then comparing, is that the reason of any issue ? – George.G May 05 '21 at 18:09
  • @George.G There is no benefit to storing a date value in a text column. Store a date value in a column of a data type akin to the SQL-standard type `DATE`. Reread my last sentence in my answer: No strings needed. – Basil Bourque May 06 '21 at 04:36
0

How are the 'dateParam' and 'ps' connected? Is the dateParam used as a filter?

It looks like you have a problem within prepared statement, the exception is most likely coming from Oracle DB https://www.techonthenet.com/oracle/errors/ora01861.php