• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to enter date object in ms access database

 
shahidrasul shahid
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to make query for date object to enter in ms access database?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34234
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic