简体   繁体   English

如何将空的 DateTime 插入 MS Access 数据库?

[英]How do I insert an empty DateTime to a MS Access database?

My problem is that I don't know how to handle an empty LocalDate object.我的问题是我不知道如何处理空的 LocalDate 对象。

I have an Excel sheet which has a date column.我有一个 Excel 工作表,其中有一个日期列。 The format is like this:格式是这样的:

19693112 or 00000000 (if the date is not known yet) 19693112 或 00000000(如果日期未知)

Inside my java application I change the format of the data and upload it to a database.在我的 Java 应用程序中,我更改了数据的格式并将其上传到数据库。

I first convert the String to the format I need (which is 31.12.1969).我首先将字符串转换为我需要的格式(即 31.12.1969)。

I'm able to insert the first format into my database without problems.我可以毫无问题地将第一种格式插入到我的数据库中。 but the second one never works because my conversion class converts it to 00.00.0000 and this is not accepted by MS Access.但第二个永远不会工作,因为我的转换类将其转换为 00.00.0000 并且这不被 MS Access 接受。

I already tried to set the value I want to "null" but the database can't handle this value for a date field.我已经尝试将值设置为“null”,但数据库无法处理日期字段的此值。

Is there a way that the value inside the database displays just an empty date field?有没有办法让数据库中的值只显示一个空的日期字段?

Thanks for any help.谢谢你的帮助。

public static void insertNewPensioners(List<Pensioner> newPensioners) {

    if (cell.getColumnIndex() == indexe.get(19)) { //checks if correct excelcell is used
        String cellValue = cell.getStringCellValue();
        if (cellValue.matches("0+")) {
            resignationDate = null;
        } else {
            resignationDate = DateFormatter.formatDate(cellValue);
        }

        pensionerExcel.add(new Pensioner(pknr, idkz, resignationDate));

        insertNewPensioners(newPensioners);
        
        public static void insertNewPensioners (List < Pensioner > newPensioners) {
            try {
                connect(pathDB);
            } catch (Exception e) {
                Alerts.connectionError();
                e.printStackTrace();
            }

            for (int i = 0; i < newPensioners.size(); i++) {
                String insert = ("Insert into Pensionär (PKNR, IDKZ, ResignationDate) VALUES " +
                        "(?,?,?)");
                try {
                    ps = connection.prepareStatement(insert);
                    ps.setInt(1, newPensioners.get(i).getPensionInsuranceNumber());
                    ps.setInt(2, newPensioners.get(i).getIdkz());
                    ps.setDate(3, Date.valueOf(newPensioners.get(i).getResignationDate()));
                    ps.executeUpdate();
                    validRows++;
                } catch (Exception e) {
                    Alerts.wrongValues();
                    e.printStackTrace();
                }
            }
            close();
        }

null would give a nullpointer exception again. null 会再次给出空指针异常。 Also he checks for an empy string but what I want is to "convert" 00000000 to an empty LocalDate.他还检查了一个空字符串,但我想要的是将 00000000“转换”为空的 LocalDate。 I need to know how I can get a format which can be used in my ms access database.我需要知道如何获得可以在我的 ms access 数据库中使用的格式。

But the fact is - as mentioned - that "an empty date" in an Access table is Null - there is no other option.但事实是 - 如前所述 - Access 表中的“空日期”为Null - 没有其他选择。

So:所以:

  • modify your code to insert Null for your empty dates修改您的代码为您的空日期插入Null
  • adjust your code to expect and accept Null for an empty date and convert this to whatever your code accepts (00000000).调整您的代码以期望并接受空日期的Null并将其转换为您的代码接受的任何内容(00000000)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM