• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

insert time only in database from JSpinner

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, am working on a project that requires user to select desired time. i have used a JSpinner to enable one select time. i have used the customized my code for the Spinner as shown below
(under code customizer)


when i get the value on the spinner e.g

both time and date are inserted in the database but i want to insert time only.
kindly help me please.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears you're converting the date to a String before inserting it into the database. It's generally a bad idea to use String variables to represent data which is of some other data type already supported by the database.

You don't show your database code to be commented on, but PreparedStatement has a setTime() method which is what you should be using to write data into your database column which is of type TIME. (You do have a TIME column, don't you?)
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i do have a Time column in the database
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do i write only time in the database since even when i use the setTime() method the value from the spinner still has both the date and time?
please
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To use the setTime() method you need to give it a java.sql.Time object, which you'll have to create based on your java.util.Date object. The purpose of the setTime() method is to set a TIME column so you can assume that the writers of the database driver are going to do that.

But are you worrying that because the object in the Java code has both date and time, then the setTime() method will transfer both of those things to the database? You only have a problem if that actually happens -- and you haven't tried it yet, right? There are always plenty of real problems to solve, don't spend your time worrying about problems which haven't happened yet.
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have already tried that and it has happened but by then i was not using the setTime() method.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:i have already tried that and it has happened but by then i was not using the setTime() method.


A TIME column and using the getTime() and setTime() methods (with a java.sql.Time object) is exactly what you need to store a time value in the database. You should always use the appropriate methods and objects in order to save the appropriate value in your database (date, time or timestamp).
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sheriff, i got it working.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:Thanks Sheriff, i got it working.


Glad to hear you got it working

Could you share some details about the solution you have implemented? Might be helpful to other ranchers...
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i customized my spinner custom code to this


then i created a java.sql.Time object
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
however the only other problem i have is that it is inserting the current time rather than the selected time.
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i need to figure out how i get the value selected in the spinner via getValue hopefully
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right. But you'll need to keep a reference to the SpinnerDateModel so that you can call its getValue() method.
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you please help me out
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this line of code



you created a SpinnerDateModel, which is what your GUI works with. If you want your code to work with it as well (and you do want that) then you need a reference to it. Which you didn't do. So change your code to keep a reference:



Now you have a variable on which you can call getValue() to find out what the latest value of the Spinner was.


 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have done it but am getting a ClassCastException: java.util.Date cannot be casted to java.sql.Time

then

i belief the value in the spinner include date, time and day and i cannot see how i will take time alone as i had done previously using sqlTime to take the current time only
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:i belief the value in the spinner include date, time and day and i cannot see how i will take time alone as i had done previously using sqlTime to take the current time only


Exactly like you did in your previous code snippetBut instead of creating a new Date instance, you use the value from the reference variable myTime.
 
james kinyua
Greenhorn
Posts: 28
1
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally here is the solution. i just need to format my spinner value using SimpleDateformat

thank guys
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:finally here is the solution. i just need to format my spinner value using SimpleDateformat


Thanks for coming back and sharing your solution! Might be helpful for other ranchers too. Have a cow!
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

james kinyua wrote:finally here is the solution...


Thanks for that, but I DO worry about it.

Why? Because it looks like you're setting a clock time (or "local" time), which is unlikely to scale well.

Example: Suppose your database is currently in Toronto, and you decide to open a Vancouver office (3 hour time difference). What do you do?
Force everyone that doesn't happen to be in Toronto to convert it?

Your current conversion will work fine for submitting times TO your database - ie, they will be converted to a local clock time - but what about reading times FROM it?

The great thing about a Java Date (which, as you already know, includes the time of day) is that it's a value - and it's the same value for EVERYONE, anywhere in the world; even in a database.

As soon as you start mucking about with it though, all bets are off.

HIH

Winston
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

james kinyua wrote:finally here is the solution...


Thanks for that, but I DO worry about it.


I agree!

In all previous code snippets you were using the java.sql.Time class to store your time value. Why did you finally decide to save a formatted String value instead?
 
reply
    Bookmark Topic Watch Topic
  • New Topic