java - Get data between the dates -


string date1 = (((jtextfield)jdatechooser1.getdateeditor().getuicomponent()).gettext()); string date2 = (((jtextfield)jdatechooser2.getdateeditor().getuicomponent()).gettext());  string query="select * work_hours id ="+a+" , date >= "+date1+" , date <= "+date2+" "; resultset rs = db.select(query); 

here date1 , date2 jdatechooser, taken user.

 error:     have error in sql syntax; check manual  corresponds mariadb server version right syntax use  near 'date >= jun 12, 2017 date <= jun 14, 2017' @ line 1 

date-time types

for date-time values, use date-time data types define column, , use date-time classes in java. job of jdbc driver mediate between these types.

you trying pass strings rather date-time objects.

half-open logic

in date-time work, use half-open approach beginning inclusive while ending exclusive. lunch starts @ noon , runs to, not include, first moment of 1 pm. week starts @ monday , runs to, include, following monday.

select * tbl_ when_ >= ?   -- pass start moment. inclusive. ,   when_  < ?   -- pass stop moment.  exclusive. ; 

the sql command between “closed” meaning both beginning , ending inclusive; not date-time work.

parse strings date-time

you need transform user-input date-time objects. may want parse string types user. or may want use date-time widget. in case, parsing strings apparently needed. search stack overflow datetimeformatter find hundreds of existing questions , answers.

sql & jdbc

the instant class in java represents moment on timeline in utc. equivalent legacy java.util.date class finer resolution of nanoseconds rather milliseconds.

apply time zone zoneid zoneddatetime object. equivalent 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 lowercase letter.

avoid naming columns in database reserved words. easiest way entirely avoid reserved words append trailing underscore names of database objects. sql standard explicitly promises never use trailing underscore.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -