The Artima Developer Community
Sponsored Link

Java Answers Forum
Date insertion problem in database

12 replies on 1 page. Most recent reply: Jan 11, 2007 7:15 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 12 replies on 1 page
Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Date insertion problem in database Posted: Dec 11, 2006 2:25 PM
Reply to this message Reply
Advertisement
I m trying to insert two dates in a MSAccess table,If I insert
String s=2/1/2006 assuming that the date format is (dd mm yyyy)the date is shown in dd mm yyyy format(2/1/2006) in MSAccess. BUt if I insert 13/1/2006(dd mm yyyy)
the date is shown in mm dd yyyy(1/13/2006). Then how can I confirm which format to use during insertion. It seems dat MSAccess uses mm dd yyyy format by default.If it is so the first date shud be displayed as 1/2/2006(mm dd yyyy).
The MSAccess DB seems to mix up date of month with month during insertion.
Now I cant understand in which format shud I insert date in MSAcccess.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Dec 11, 2006 11:01 PM
Reply to this message Reply
Obviously there is some logic working trying to understand what you inserted.

In case "2/1/2006" It takes 2 as month and 1 as day ("mm/dd/yyyy").

In case "13/1/2006" the logic realises that 13 can't be a month and parses the incoming string as "dd/mm/yyyy".

But: It seems the format Access expects by default (if month and day are <= 12) depends on your country settings.
My computer has german settings ("dd/mm/yyyy"), so it treats "13/01/2006" exactly like "02/01/2006"


One solution would be to use a fail safe country setting independend format like yyyy-mm-dd.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Date insertion problem in database Posted: Dec 12, 2006 11:51 PM
Reply to this message Reply
I hav confirmed my regional setting which is- English(US)
from control panel-regional and language option.And the date is short date in MM/dd/yyyy format. But the problem still exists.If I insert any date of month between 1-12(e.g 1/2/2004(dd mm yyyy),the Access DB takes it as month and sets it as mm dd yyyy format(1/2/2004).But if I insert any date of month above 12(e.g 13/2/2004 - dd mm yyyy) the Access DB takes it as date of month and sets it in mm dd yyyy (2/13/2004)format. Now wud pls tell me the way to ged rid of it. Shud I insert date in mm dd yyyy format directly instead of dd mm yyyy format? Why is the Access Db not recognizing my given date format(dd mm yyyy) and the format it gets from the System(MM dd yyyy).I m stuck here in my assignment.
Little abt my programe-I have 2 textfields one for taking a date for a Task Start date and a duration textfield which takes duration period for that Task. Later this duration period is added to the Task Start date to calculate the Task Finished Date.Now just imagine if I input the above date value-1/2/2004(dd mm yyyy) assuming 1 is date of month here and if Access Db takes it as 1/2/2004(mm dd yyyy)..then what will be the Task Finished date after calculation. Pls help me. Tell me is there any way out..THNX.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Dec 13, 2006 2:57 AM
Reply to this message Reply
Use "mm/dd/yyyy" or "yyyy-mm-dd"

Acces takes the format from the system, no matter whar you set in the table editor's field properties, that's just how Access formats the value when you display it.

I really don't understand your problem.
So your Start date is 2004-01-02 (so we don't have to mention the format everytime).

What do you want to save in your db?

Do you want to save the start date in one field and the end date in another?

Or do you want so save the start date in one field and the duration in another?

If you want to add the duration to the start date, where should that happen? In your program or when you make a selection on your database?


Note: Saving "1/2/2004" in "mm/dd/yyyy" or "2/1/2004" in "dd/mm/yyyy" or "2004-01-02" leads to exactly the same value saved in the database. The format just defines how a date is displayed or how a String is parsed.

You won't be able in any way to insert a month > 12, that's just not possible.

While parsing, Access' Logic notices that a month > 12 is not possible and exchanges day with month in that case.
A duration is not a date, so if you want to store "13 months", then use another datatype.

If you just want to store the end date, the sum of 2004-01-02 and 0000-12-00 must be 2005-01-02 of course. It can't be 2004-13-02. That's an invalid value which probably will be converted to 2004-02-13 (I didn't test it).

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Date insertion problem in database Posted: Dec 14, 2006 6:35 AM
Reply to this message Reply
I hav 3 JTextFields in a JFrame-Task Name, Task Start Date,Task Duration. I hav left no chance for the user to enter a task end date which is earlier than the Task Start date. The user will give the Task Start date and Duration in d interface.The programe will take the duration period and add this to the Task Start Date and then insert this calculated task end date in Access DB. The Access DB Table has three fields-Task Name,Task Start date, Task End date.
After the insertion, the programe will search each task Start Date and add the Date of month from the task start date and end date in a two dimensional array for another purpose.The same way it will add only month from the Task start date field and task end date field and put it in another two dimensional array. I did it by get(Calender.Month)..e.t.c. of Calender class.
Now if I input date- 1/2/2004 (dd/mm/yyyy)by the text field,Access database insert it in 1/2/2004 but in (mm/dd/yyyy)format. Now u know dat my programe will next take the Date of month to add it to a 2 dimensional array,in this case it will take 2 as Date of month and insert it in d array. But it shud have taken 1 as date of month bcos I input 1/2/2004 in dd/mm/yyyy format.If I use get() of Calender class, doesnt it return 2 as Date of Month bcos the Access sets the date as 1/2/2004 in (mm/dd/yyyy). But I remind u dat I inputed it in 1/2/2004(dd/mm/yyyy)format.
I think I hav made u get my problem.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Dec 14, 2006 1:03 PM
Reply to this message Reply
How did you tell access that your date format uses "dd/mm/yyyy"?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Dec 14, 2006 1:07 PM
Reply to this message Reply
No matter what you input in your text field, you must use "mm/dd/yyyy" for the SQL String.

If you just put "1/2/2004" into the String without any description, Access has no way to find out if you mean "dd/mm/yyyy" or "mm/dd/yyyy", so it uses it's default format.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Date insertion problem in database Posted: Dec 14, 2006 1:42 PM
Reply to this message Reply
Do you mean that I have to use mm/dd/yyyy format in the SQL String.If so, do I need to parse the string for geting mm/dd/yyyy format(and again format that date to string),if the string date is in another format.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Date insertion problem in database Posted: Dec 18, 2006 1:03 PM
Reply to this message Reply
I hav posted this in another thread but didnt get reply.Pls I m sorry for doble posting but I m stuck here in my progrmme. I seek ur help.My Follwing programe throws an Exception which is-java.sql.SQLException.....Data Type mismatch in criteria expression.In My program there are two customiazed class- 1. DBase() and 2-JDateChooser. DBase is used to connect database.JDateChooser is a class which creates a textfield with a calendar besides it. After selecting a date frm dat calendar it shows the date in MMM dd,yyyy format. It also returns the date by getDate() in dat format. I tried to get dat date and format it in dd mm yyyy format. But the above exception occurs.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import tools.diit.*;
import java.sql.*;
import java.text.*;
import java.util.Date;
import com.toedter.calendar.*;
 
public class SimpleDatabaseExm extends JFrame{
	
 
 JDateChooser dtc= new JDateChooser();
 JButton btn=new JButton();
 
 String tableName = "Stat";
 String primaryKey= "ID";
 
Statement stmt;
ResultSet rs;
 
Calendar cl=new GregorianCalendar();
 
SimpleDatabaseExm (){
 
stmt = new DBase().getStatement("./Data/Diit13.mdb","",""); 
 
this.setSize(300,300);
this.setLocation(300,150);	
this.getContentPane().add(dtc,BorderLayout.NORTH);
this.getContentPane().add(btn,BorderLayout.SOUTH);
 
btn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
      
      Date stdate=dtc.getDate();
      getDate(stdate);
      
      }
    });
	
}// end constr
 
 
public static void main(String[] args) 
{ 
 SimpleDatabaseExm dbconn=new SimpleDatabaseExm(); 
 dbconn.setVisible(true); 
 
}
 
 public void refresh()
   {
			try{
					rs =stmt.executeQuery("SELECT * FROM " + tableName);
					rs.next();
			}catch(SQLException sqle){
				System.out.println("ERROR REFRESH : " + sqle);
				}
   }//End of Method refresh()
 
 
public void getDate(Date d3)
{
//refresh();	
	
SimpleDateFormat sdf = new SimpleDateFormat("dd mm yyyy");
 
 
try{
	String s=sdf.format(d3);
	
	String s3=sdf.format(new Date());
		
	System.out.println("Start date-"+ s +"End Date"+ s3);
	
	String st="INSERT INTO Stat(Year,Income) VALUES('"+s+"','"+s3+"')";
	stmt.executeUpdate(st);
	refresh();
 
   	}
   	catch(SQLException sqle)
    {  	System.out.println("ERROR DISPLAY : " + sqle); }
	
	catch(NullPointerException ex)
	{	System.out.println("Null Pointer Ex");         }
	
 
 
	
}
 
 
}
 

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Date insertion problem in database Posted: Dec 19, 2006 2:03 AM
Reply to this message Reply
> My Follwing programe throws
> an Exception which is-java.sql.SQLException

A SQLException indicates that there is a problem in the SQL you are trying to run.

> SimpleDateFormat sdf = new SimpleDateFormat("dd mm
> yyyy");
>
>
> try{
> String s=sdf.format(d3);
> String s3=sdf.format(new Date());
> String st="INSERT INTO Stat(Year,Income)) VALUES('"+s+"','"+s3+"')";
> stmt.executeUpdate(st);

If the field names "Year" and "Income" are anything to go by, then the values s and s3 would appear to be entirely unsuitable values to insert into them.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: Date insertion problem in database Posted: Dec 19, 2006 9:02 AM
Reply to this message Reply
The fields "Year" and "Income" are set as "Date/Time" format in MSAccess. My system date format is "MM dd yyyy".MSAccess also applies this format by default.Why does the exception occur then?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Jan 11, 2007 7:13 AM
Reply to this message Reply
Why did you use "mm dd yyyy" and not "dd/mm/yyyy"?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Date insertion problem in database Posted: Jan 11, 2007 7:15 AM
Reply to this message Reply
Sorry, made a mistake.


Why did you use "mm dd yyyy" and not "mm/dd/yyyy"?

Please print out the final sql string and post it here.

Flat View: This topic has 12 replies on 1 page
Topic: Date insertion problem in database Previous Topic   Next Topic Topic: java 3d

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use