[英]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. 我想将日期选择器中的文本设置为SQL查询的结果。 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; 我尝试使用设置我的DatePicker的值;
deadlineDatePicker.setText(deadlineDatePrompt);
This however does not work as setText isn't defined for DatePicker. 但是,这没有用,因为没有为DatePicker定义setText。 Changing setText()
to setValue()
does not work either as deadlineDatePrompt
is stored as a string. 将setText()
更改为setValue()
也不起作用,因为deadlineDatePrompt
存储为字符串。 Changing it to a LocalDate
does not fix the issue as then rs.getString
does not work. 将其更改为LocalDate
不能解决该问题,因为rs.getString
不起作用。
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? 是否有解决方法,这样我可以获得数据库中设置的日期的结果并将其显示在DatePicker中?
You can retrieve the value from the database as a java.sql.Date
. 您可以从数据库中检索值作为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. DatePicker
需要一个java.time.LocalDate
,因此您需要对其进行转换:这很简单,因为java.sql.Date
定义了toLocalDate()
方法。
So 所以
java.sql.Date deadlineDatePrompt = rs.getDate("Deadline");
deadlineDatePicker.setValue(deadlineDatePrompt.toLocalDate());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.