1
String Date1 = (((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText());
String Date2 = (((JTextField)jDateChooser2.getDateEditor().getUiComponent()).getText());

String query="SELECT * FROM work_hours WHERE ID ="+A+" AND Date >= "+Date1+" AND Date <= "+Date2+" ";
ResultSet rs = db.Select(query);

Here Date1 and Date2 is the Jdatechooser, taken from user.

 Error:   
 You have an error in your SQL syntax; check the manual that
 corresponds to your MariaDB server version for the right syntax to use
 near 'Date >= Jun 12, 2017 Date <= Jun 14, 2017' at line 1
piet.t
  • 11,718
  • 21
  • 43
  • 52
  • You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Date >= Jun 12, 2017 BETWEEN Date <= Jun 14, 2017' at line 1 – Asad Mughal Jul 28 '17 at 12:13
  • https://www.w3schools.com/sql/sql_dates.asp – Pavneet_Singh Jul 28 '17 at 12:17

5 Answers5

2

Date-time types

For date-time values, use date-time data types to define your column, and use date-time classes in Java. The job of your JDBC driver is to mediate between these types.

You are trying to pass strings rather than date-time objects.

Half-Open logic

In date-time work, use Half-Open approach where the beginning is inclusive while the ending is exclusive. So lunch starts at noon and runs up to, but does not include, the first moment of 1 PM. A week starts at Monday and runs up to, but does include, the following Monday.

SELECT * FROM tbl_
WHERE when_ >= ?   -- Pass start moment. Inclusive.
AND   when_  < ?   -- Pass stop moment.  Exclusive.
;

The SQL command BETWEEN is “closed” meaning both the beginning and ending are inclusive; not good for date-time work.

Parse strings to date-time

You need to transform your user-input into date-time objects. You may want to parse a string types by user. Or you may want to use a date-time widget. In your case, parsing strings is apparently needed. Search Stack Overflow for DateTimeFormatter to find hundreds of existing Questions and Answers.

SQL & JDBC

The Instant class in Java represents a moment on the timeline in UTC. Equivalent to the legacy java.util.Date class but with a finer resolution of nanoseconds rather than milliseconds.

Apply a time zone ZoneId to get a ZonedDateTime object. Equivalent to the legacy class GregorianCalendar.

ZonedDateTime zdt = ZonedDateTime.parse( input , … ) ;
myPreparedStatement.setObject( … , zdt.toInstant() ) ;

And…

Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime zdt = instant.atZone( ZoneId.of( "America/Montreal" ) ) ;

Tips

Observe naming conventions. In Java, variables start with a lowercase letter.

Avoid naming columns in database with reserved words. Easiest way to entirely avoid all reserved words is to append a trailing underscore to all the names of all your database objects. The SQL standard explicitly promises to never use a trailing underscore.

Rick James
  • 135,179
  • 13
  • 127
  • 222
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Use STR_TO_DATE function;

String query="SELECT * FROM work_hours WHERE ID ="+A+" AND Date >= STR_TO_DATE("+Date1+") AND Date <= STR_TO_DATE("+Date2+") ";
Gökhan Polat
  • 431
  • 4
  • 9
0

Try to qoute the dates 'date1'.

String query="SELECT * FROM work_hours WHERE ID ="+A+" 
AND Date >= '"+Date1+"' AND Date <= '"+Date2+"' "; 
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

You can try to use 'BETWEEN'.

Exempel:

SELECT * FROM table_name WHERE date BETWEEN '2017-07-01 07:07:07' AND '2017-07-31 07:07:07';

I hope I was able to help you.

SCHREDDO
  • 41
  • 5
  • You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '12, 2017 AND Jun 14, 2017' at line 1 – Asad Mughal Jul 28 '17 at 12:36
  • I think you forget the `'` around the date in SQL query. – SCHREDDO Jul 28 '17 at 13:19
  • 2
    The SQL command `BETWEEN` is “closed” meaning both the beginning and ending are inclusive. This is *not* what is best used for date time work. In date-time work, use Half-Open approach where the beginning is *inclusive* while the ending is *exclusive*. So lunch starts at noon and runs up to, but does not include, the first moment of 1 PM. A week starts at Monday and runs up to, but does include, the following Monday. – Basil Bourque Jul 28 '17 at 18:54
0

To solve this issue you need to accomplish to steps:

  • Proper date format from JAVA snippet
  • Add single quotes wrapping dates in SQL script

I'm not sure how you format a date variable en JAVA to return the ANSI date standard 'YYYYMMDD'

The SQL script needs to look like this:

String query="SELECT * FROM work_hours WHERE ID ="+A+" AND Date >= '"+Date1+"' AND Date <= '"+Date2+"' ";
ResultSet rs = db.Select(query);
Horaciux
  • 6,322
  • 2
  • 22
  • 41