简体   繁体   English

使用MySQL查询的结果设置DatePicker值

[英]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());

You should read 'Deadline' field as a date using getDate : 您应该使用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. 请注意,get Date返回的java.sql.Date不是java.util.Date,因此您需要将一个转换为另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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