aspose file tools*
The moose likes JDBC and the fly likes how to enter date object in ms access database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to enter date object in ms access database" Watch "how to enter date object in ms access database" New topic
Author

how to enter date object in ms access database

shahidrasul shahid
Ranch Hand

Joined: Jan 28, 2008
Posts: 62
how to make query for date object to enter in ms access database?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Shahidrasul,
Your best bet is to use a PreparedStatement for your insert. This lets you type "stmt.setDate(javaDate)" rather than have to fiddle with date formats.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Abhin Balur
Greenhorn

Joined: Feb 18, 2008
Posts: 21
HI,

Just make use of this function to enter date into MsAccess database.
public static java.sql.Date getCurrentJavaSqlDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}

So now when you want to make an entry of date into database just call this function and store it in for example say java.sql.Date object, and set it using the prepared statment. For example suppose ps is your prepared statement then ps.setDate(int,your date object) should set the date.

A sample code below to help understand:-


String sol= "INSERT into storage1([Items],[Spaceocc],[Date]) VALUES (?,?,?)";

ps2=con.prepareStatement(sol);


if(proceed){

ps2.setString(1,"Set");
ps2.setInt(2, 0);
java.sql.Date date = getCurrentJavaSqlDate();
ps2.setDate(3,date);

ps2.executeUpdate();
}


Hope this works.
Abhin Balur
Greenhorn

Joined: Feb 18, 2008
Posts: 21
HI,

Just make use of this function to enter date into MsAccess database.
public static java.sql.Date getCurrentJavaSqlDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}

So now when you want to make an entry of date into database just call this function and store it in for example say java.sql.Date object, and set it using the prepared statment. For example suppose ps is your prepared statement then ps.setDate(int,your date object) should set the date.

A sample code below to help understand:-


String sol= "INSERT into storage1([Items],[Spaceocc],[Date]) VALUES (?,?,?)";

ps2=con.prepareStatement(sol);


if(proceed){

ps2.setString(1,"Set");
ps2.setInt(2, 0);
java.sql.Date date = getCurrentJavaSqlDate();
ps2.setDate(3,date);

ps2.executeUpdate();
}


Hope this works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to enter date object in ms access database