简体   繁体   English

从jDateChooser获取价值并保存到MS sql DB

[英]getting value from jDateChooser and saving to MS sql DB

I have two jDateChooser on my dialog , I want to save to MS-SQL DB having issue with that data types. 我的对话框中有两个jDateChooser,我想保存到MS-SQL DB中该数据类型有问题的数据库。 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. 我只能在将数据类型转换为DB中的nvarchar并将值转换为从jDateChooser返回的字符串时才能执行此操作。

在此处输入图片说明

// I can save in this way but I it doesn't use jDateChooser; //我可以用这种方式保存,但是我不使用jDateChooser;

   java.util.Date utilDate = new java.util.Date();
   java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());

// I cant save the date with jDateChooser //我无法使用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);

// //

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. 从代码来看您发布它看起来像jDateChooser3.getDate()返回一个java.util.Date而实例java.sql.Date(millis)构造预计的日期/时间为long毫秒值。

Use this code and it will work: 使用此代码,它将起作用:

java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate().getTime());

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: 由于它来自日期选择器组件,因此无效输入很可能导致返回的日期为null ,因此您可能还需要检查以下内容:

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
}

Right click on jDateChooser. 右键单击jDateChooser。 Go to the Properties and dateFormatString. 转到属性和dateFormatString。 Set this format: dd-MM-yyyy. 设置此格式:dd-MM-yyyy。

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

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