从jDateChooser获取值并保存到MS sql DB [英] getting value from jDateChooser and saving to MS sql DB
问题描述
我的对话框中有两个jDateChooser,我想保存到有这些数据类型问题的MS-SQL DB。不知道如何解决这个问题!我只能在数据库中将数据类型转换为nvarchar并将值转换为从jDateChooser返回的字符串时执行此操作。
I have two jDateChooser on my dialog , I want to save to MS-SQL DB having issue with that data types. Any idea how to fix this issue ! I can only do this when i convert data type to nvarchar in DB and convert the value to string which returns from jDateChooser.
//我可以这样保存,但我不使用jDateChooser;
// I can save in this way but I it doesn't use jDateChooser;
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
//我无法用jDateChooser保存日期
// I cant save the date with jDateChooser
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate());
//只有我找到的方式
// Only Way I found
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sd = dateFormat.format(jDateChooser3.getDate());
obj.setStartDate(sd);
//
推荐答案
从您发布的代码判断它看起来像 jDateChooser3.getDate()
返回 java.util.Date
实例,而 java.sql.Date(millis)
构造函数则期望日期/时间为 long
毫秒价值。
Judging from the code you posted it looks like jDateChooser3.getDate()
returns a java.util.Date
instance while the java.sql.Date(millis)
constructor expects the date/time as a long
milliseconds value.
使用此代码它将起作用:
Use this code and it will work:
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate().getTime());
由于它来自日期选择器组件,无效输入最有可能导致 null
返回日期,因此您可能还需要检查:
Since it comes from a date chooser component, invalid input most likely results in null
returned date, so you might want to also check on that:
java.util.Date d = jDateChooser3.getDate();
if (d == null) {
System.out.println("No date specified!");
} else {
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// Do something with sqldate
}
这篇关于从jDateChooser获取值并保存到MS sql DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!