This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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
 
Similar Threads
Date Format
date and time in sql
how to enter date object in ms access database
JDBC MSAccess
Date format