【问题标题】:how to insert in a date/time ms access field using java sql INSERT query如何使用 java sql INSERT 查询插入日期/时间 ms 访问字段
【发布时间】:2012-12-31 18:51:36
【问题描述】:

我想在 MS Access 中添加一些日期和时间,但我的日期和时间变量是一个字符串。即

String dt="12/2/2014 9:00 PM"; //this is selected from a calender component and a ComboBox

而 MS Access 字段的类型是(日期/时间)。如何将我的字符串转换为日期/时间类型,以便将其插入到字段中?你能用几行代码说明一下吗?因为我不是java专家。像我想要的东西:

step1:将String转换为日期时间字段

step2: statement.executeUpdate(插入表(Date-Time) Values(??????)

【问题讨论】:

  • 你的问题不是很清楚/可读...

标签: java sql ms-access


【解决方案1】:

步骤:1 将字符串转换为日期时间字段:

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
String stringDate = "12/2/2014 9:00 PM";
java.util.Date date = df.parse(stringDate);

这将为您提供字符串中的日期。 有关 SimpleDateFormat 的更多详细信息See the API docs

第 2 步:

String query = "Insert into table MyTable(dateColumn) Values(?)";
PreparedStatement ps  = connection.prepareStatement(query);
ps.setTimestamp(1,new java.sql.Timestamp(date.getTime()));
ps.executeUpdate();

这里还有一些details on using prepared statements

【讨论】:

  • 我已经使用了上面的代码。工作正常,但如何使用这个条件?即“插入表 MyTable(dateColumn) Values(?) where title='abc'”;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 2011-10-31
  • 2019-08-18
  • 1970-01-01
相关资源
最近更新 更多