如何使用java sql INSERT查询在日期/时间ms访问字段中插入 [英] how to insert in a date/time ms access field using java sql INSERT query
问题描述
我想在MS Access中添加一些日期和时间,但我的日期和时间变量是一个字符串。即
I want to add some date and time in the MS Access, but my date and time variable is a string. i.e
String dt="12/2/2014 9:00 PM"; //this is selected from a calender component and a ComboBox
虽然MS Access字段的类型是(日期/时间)。如何将我的字符串转换为日期/时间类型,以便它可以插入到字段?你能用一些代码来说明吗?因为我不是java的专家。像我想要的东西:
While MS Access field's type is (Date/Time). How to convert my string to date/time type so that it can be inserted to the field? Can you please illustrate with some line of code? As I am not an expert in java. Something like what I want:
step1:将字符串转换为日期时间字段
step1: Converting String to date time field
step2:statement.executeUpdate(插入表格(日期 - 时间)值(??????)
step2: statement.executeUpdate(Insert into table (Date-Time) Values(??????)
推荐答案
步骤: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的详细信息查看API文档
This will give you the date from string. For more details on 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();
这里还有一些使用预准备语句的详细信息
这篇关于如何使用java sql INSERT查询在日期/时间ms访问字段中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!