简体   繁体   中英

Set DatePicker Value With Result from MySQL Query

I want to set the text inside a date picker to the result of an SQL query. I do the following to get the date;

deadlineDatePrompt = rs.getString("Deadline");

This obviously stored the date as a String. I try to set the value of my DatePicker using;

deadlineDatePicker.setText(deadlineDatePrompt); 

This however does not work as setText isn't defined for DatePicker. Changing setText() to setValue() does not work either as deadlineDatePrompt is stored as a string. Changing it to a LocalDate does not fix the issue as then rs.getString does not work.

Is there a work around for this so that I can get the result of the date set inside my database and display it inside the DatePicker?

You can retrieve the value from the database as a java.sql.Date . A DatePicker requires a java.time.LocalDate , so you need to convert it: this is pretty easy as java.sql.Date defines a toLocalDate() method.

So

java.sql.Date deadlineDatePrompt = rs.getDate("Deadline");
deadlineDatePicker.setValue(deadlineDatePrompt.toLocalDate());

You should read 'Deadline' field as a date using getDate :

 rs.getDate("Deadline");

Be aware that get Date return java.sql.Date not java.util.Date so you need to convert one to another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM