0

I'm developing a program that prompt the user to select time for his/her stay on the hotel, and i'm using a JSpinner to do this. But i don't know how to extract time from JSpinner and insert it to MySQL database on the time format. Please help and give me some example. Sorry for noob question

1 Answer 1

3
  • retrieve the data from the spinner (JSpinner#getValue) and convert it to a time object. The conversion will depend on what data is stored in your spinner model. The SpinnerDataModel might be an interesting model to back your spinner,
  • send the query to your database on a background thread, e.g. by creating a Runnable and using a new Thread to execute this.

Examples can be found in the JSpinner tutorial. For example (copy pasted from that tutorial) if you use the SpinnerDateModel, you can use

SpinnerDateModel dateModel = ...;
...
setSeasonalColor(dateModel.getDate());

which allows you to retrieve a Date object directly from the model, without a conversion.

Further, the Swing concurrency tutorial explains why you should update your db on a background thread. It also contains code snippets on how to execute something on a background thread from the EDT. But in case you simply need to update the db, and nothing in the UI must be updated, there is not much need to opt for the SwingWorker class which is used in that sample code. In that case, you can simply start a new Thread as I stated above. The net is filled with examples on how to achieve that.

Sign up to request clarification or add additional context in comments.

4 Comments

Is their a simpler way to do this? i don't want to do this with thread i'm just new to java, i don't know how to use thread yet. My program is just simply a form for hotel guest that requires a time in and compute time of stay that's why i need time input and i figure out that SpinnerDataModel can avoid much risk from the error of the user
don't know how to use Thread yet ... nothing as good to learn something new then in a project where you actually need it.
with this code setSeasonalColor(dateModel.getDate()); can i store the date object directly to the MySQL database?
Yes. But as said before, it is best to perform the database update on a background thread and not on the Event Dispatch Thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.