1
static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd- MMM-yyyy");
static String date = simpleDateFormat.format(new Date());

private static final String SELECT_BY_AUDIT_KEY_QUERY = "SELECT * FROM 
     t02 WHERE t02_create_date = " + date;

This outputs as :

SELECT * FROM t02_transaction_log WHERE t02_create_date = 03-Jul-2018

What i want is this :

SELECT * FROM t02_transaction_log WHERE t02_create_date = '03-Jul-2018'

How can i achieve this get the date in between single quotes?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
vjanu
  • 65
  • 10
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 04 '18 at 00:20

3 Answers3

5

You should be using a prepared statement here:

String sql = "SELECT * FROM t02 WHERE t02_create_date = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDate(1, new Date());
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    // process result set
}

While statements are important for preventing things like SQL injection, they are equally useful because they free you from having to worry about formatting details in your query. In the query string above, we just represent the date by a ? placeholder, and let the driver handle marshaling the current date into the correct format to be sent to Oracle.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • If date format is differ in database then what happen? Assume t02_create_date is like yyyy-mm-dd format in this case how your prepare statement will work? – Gaj Jul 03 '18 at 10:50
  • 1
    @Gaj That doesn't matter, because each database vendor provides a JDBC driver which "knows" what the correct date format going out to the database should be. So, the answer to your question is, we don't know (or really care), because the driver handles this. – Tim Biegeleisen Jul 03 '18 at 10:51
  • `setTimestamp` maybe as Gaj probably hints at time info HH:mm:ss being zeroed `by java.sql.Date`. – Joop Eggen Jul 03 '18 at 10:55
  • I don't think JDBC will take care of this. As per me, Date object will return ISO date format (YYYY-MM-DD HH:MI:SS). My question is if the create_date is stored in different format with or without timestamp then what happen? – Gaj Jul 03 '18 at 11:11
  • 1
    I'm not sure I see the concern here. The OP seems to be wanting to compare against a date, not a timestamp. – Tim Biegeleisen Jul 03 '18 at 11:12
  • 1
    @Gaj the column in the database should be a DATE. It seems you're thinking of it being a VARCHAR or something. If that is the case, your table was created *wrong*. – David Conrad Jul 03 '18 at 13:01
  • 1
    Agree that you should use a prepared statement. Provided that you are using at least Java 8 and at least JDBC 4.2 you should also not use `Date` but the modern `LocalDate` and then `ps.setObject(1, LocalDate.now(yourTimeZone));` (time zone is necessary given that it is never the same day everywhere on the globe and we want to control “which today” it’s taking). – Ole V.V. Jul 03 '18 at 13:48
1

tl;dr

get the date in between single quotes?

Don’t. You should be using smart objects, not dumb strings, to exchange date values with your database.

Use java.time classes that replaced troublesome legacy classes.

For date-only values, use LocalDate class that replaced java.sql.Date.

myPreparedStatement.setObject( 
    … , 
    LocalDate.of( 2018 , Month.JANUARY , 23 ) 
)

…and retrieval…

myResultSet.getObject( 
    … ,
    LocalDate.class 
)

java.time

The Answer by Tim Biegeleisen is correct, but uses troublesome old classes. The java.util.Date, java.sql.Date, SimpleDateFormat, and related old date-time classes were supplanted years ago by the java.time classes.

As of JDBC 4.2 and later, we can directly exchange java.time objects with the database. Use PreparedStatement::setObject and ResultSet::getObject methods.

Date-only

If your database column is of a date-only type without time-of-day and without time zone, akin to the SQL-standard DATE type, use LocalDate class. Example code:

LocalDate localDate = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;  // 2018-01-23.
String sql = "SELECT * FROM t02 WHERE t02_create_date = ? ;" ;  // Tip: Make a habit of closing your SQL properly with a semicolon. Won't hurt, and might help.
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , localDate );
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    LocalDate ld = rs.getObject( 1 , LocalDate.class ) ;  // Retrieve an `LocalDate` class.
}

Date-time

If your database column is of a date-with-time-of-day type, akin to the SQL-standard TIMESTAMP WITH TIME ZONE type, use Instant class. Example code:

LocalDate instant = LocalDate.of( 
String sql = "SELECT * FROM t02 WHERE t02_create_date = ? ;" ;  // Tip: Make a habit of closing your SQL properly with a semicolon. Won't hurt, and might help.
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , instant );
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    Instant instantRetrieved = rs.getObject( 1 , Instant.class ) ;  // Retrieve an `Instant` class.
    // You may want to adjust from UTC to some other time zone.
    ZoneID z = ZoneId.of( "Africa/Tunis" ) ;
    ZonedDateTime zdt = instantRetrieved.atZone( z ) ;  // Same moment, same point on the timeline, different wall-clock time as seen by the people of some particular region (a time zone). 
}

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Try this

 private static final String SELECT_BY_AUDIT_KEY_QUERY = "SELECT * FROM 
     t02 WHERE t02_create_date = to_date('" + date + "', 'dd-mon-yyyy')";
Gaj
  • 888
  • 5
  • 5