insert time only in database from JSpinner

james kinyua

Greenhorn

Posts: 28

1

posted 10 months ago

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.

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

posted 10 months ago

yes i do have a Time column in the database

james kinyua

Greenhorn

Posts: 28

1

posted 10 months ago

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

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

posted 10 months ago

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

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).

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.

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here