user3498019
user3498019

Reputation: 95

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. 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.

enter image description here

// 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());

// 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);

//

Upvotes: 2

Views: 31823

Answers (3)

Jalana Kannangara
Jalana Kannangara

Reputation: 11

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

Upvotes: 0

JustALady
JustALady

Reputation: 1

Right click on jDateChooser. Go to the Properties and dateFormatString. Set this format: dd-MM-yyyy.

Upvotes: 0

icza
icza

Reputation: 418695

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());

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
}

Upvotes: 5

Related Questions