Reputation: 21725
I am using MS-Access Database for my application. now I want to store current date in DB from JAVA. Following Code Segment stores date data in Database but it stores the incorrect value: 6/6/1905. The datatype in MS-Access is Date/Time. I used following code segment for setting current date:
data.setModifyDate(new Date(new java.util.Date().getTime()));
The type of modifyDate is java.sql.Date. The insert query is as follows:
String query = "INSERT INTO testTable(id, tName, modifyDate ) " +
" VALUES ("+"'"+data.getId()+"'"+","
+"'"+data.getTName()+"'"+","
+data.getModifyDate()+")";
Statement s = conn.createStatement();
s.execute(query);
TimeStamp is not supported in MS-Access 2007. So, how can I store the correct date value?
Upvotes: 1
Views: 3939
Reputation: 123809
You can use Timestamp
if you use a parameterized query to do the INSERT (which you really should be doing in any case). The following code works for me:
import java.sql.*;
public class jdbcTest {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
"Dbq=C:\\__tmp\\Database1.accdb;");
PreparedStatement s = conn.prepareStatement(
"INSERT INTO testTable (id, tName, modifyDate) VALUES (?, ?, ?)");
s.setString(1, "foo");
s.setString(2, "bar");
s.setTimestamp(3, new Timestamp(new java.util.Date().getTime()));
s.execute();
s.close();
conn.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Upvotes: 2
Reputation: 313
You are trying to insert the date/time as printed by the Date class of java. I think MSAccess needs its date/time as #YYYY-MM-DD HH:MM:SS# so try to use SimpleDateFormat to have the right format in the insert query.
Hope this helps.
Upvotes: 1